Testing if a collection contains objects based on a particular property

You could use LINQ: Assert.That(people.Any(p => p.Name == “joe”)); or, if you want to be explicit about there being exactly one person with each name: Assert.That(people.Count(p => p.Name == “joe”), Is.EqualTo(1)); If you want a better error message than “Assertion failed, expected true, was false”, you could create your own assert method. For several collection-related … Read more

How to Unit Test HtmlHelper with Moq?

Here’s another article that shows you how to achieve the same thing: public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd) { var mockViewContext = new Mock<ViewContext>( new ControllerContext( new Mock<HttpContextBase>().Object, new RouteData(), new Mock<ControllerBase>().Object), new Mock<IView>().Object, vd, new TempDataDictionary()); var mockViewDataContainer = new Mock<IViewDataContainer>(); mockViewDataContainer.Setup(v => v.ViewData).Returns(vd); return new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object); }

How do I enforce exception message with ExpectedException attribute

We use this attribute all over the place and we clearly misunderstood the second parameter (shame on us). However, we definitely have used it to check the exception message. The following was what we used with hints from this page. It doesn’t handle globalization, or inherited exception types, but it does what we need. Again, … Read more

Testing controller Action that uses User.Identity.Name

You can use this code public SomeController CreateControllerForUser(string userName) { var mock = new Mock<ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new SomeController(); controller.ControllerContext = mock.Object; return controller; } It uses Moq mocking framework, but sure you can use anything you like.

C# – Asserting two objects are equal in unit tests

You’ve got two different Board instances, so your call to Assert.AreEqual will fail. Even if their entire contents appear to be the same, you’re comparing references, not the underlying values. You have to specify what makes two Board instances equal. You can do it in your test: Assert.AreEqual(expected.Rows.Count, actual.Rows.Count); Assert.AreEqual(expected.Rows[0].Cells[0], actual.Rows[0].Cells[0]); // Lots more tests … Read more

Nunit – doesn’t discover tests [no error-message]

You must either install the NUnit VSAdapter vsix extension, or add the adapter as nuget package to your solution. The latest version is the 2.0, and the vsix is available here: https://visualstudiogallery.msdn.microsoft.com/6ab922d0-21c0-4f06-ab5f-4ecd1fe7175d And the nuget package can be found here: http://www.nuget.org/packages/NUnitTestAdapter/ More information on these options can be found in this MSDN ALM post http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/11/part-3-unit-testing-with-traits-and-code-coverage-in-visual-studio-2012-using-the-tfs-build-and-the-new-nuget-adapter-approach.aspx, … Read more

How do I set up NUnit in Visual Studio 2022?

You can add both NUnit Framework and NUnit Test Adapter using NuGet Packages. To do that, right click on your project in Solution Explorer, go to Manage NuGet packages…, in the Browse section type nunit, install NUnit package and the corresponding version adapter (NUnitTestAdapter for NUnit 2.x or NUnit3TestAdapter for NUnit 3.x).

Which is better? Unit-test project per solution or per project?

Assemblies are a packaging/deployment concern, so we usually split them out because we don’t want to deploy them with our product. Whether you split them out per library or per solution there are merits to both. Ultimately, you want tests to be immediately available to all developers, so that developers know where to find them … Read more