↓ Skip to main content

Unit Testing

Unit Test Boilerplate Generator

Unit Test Boilerplate Generator

·1 min
I love this smart template generator, saves so much time and effort. Generates a unit test boilerplate from a given C# class, setting up mocks for all dependencies. Right click an item in Solution Explorer and choose “Create Unit Test Boilerplate” . Supports Visual Studio Test, NUnit, xUnit and many mock frameworks.
Async Unit Tests

Async Unit Tests

·1 min
In theory, Async unit testing seems easy, run the Async, wait until it is finished and look at the results. But as you will find out it is not that easy. Here is the official approach to Async unit testing
Create a subset of Unit Tests with a Playlist

Create a subset of Unit Tests with a Playlist

·1 min
Visual Studio Unit Test Tools comes with another excellent feature to manage unit test as a group /subset, called as “Playlist”. A Playlist is a subset of unit test methods grouped under some category. The Playlist could be a logical subset based on modules, features, layers etc. A Playlist is useful when we want to test a particular set of test cases among all the test methods. In that case, we just create a group of those test cases which may get impacted by our current changes in the actual code, and execute them only to ensure nothing breaks.

Make Fake MVC Objects for testing

·3 mins
If you need to create some Fake MVC objects for testing MVC objects then here you are: FakeControllerContext using System.Collections.Specialized; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.SessionState; using MvcContrib.TestHelper.Fakes; public class FakeControllerContext : ControllerContext { public FakeControllerContext(IController controller) : this(controller, null, null, null, null, null, null) { } public FakeControllerContext(IController controller, HttpCookieCollection cookies) : this(controller, null, null, null, null, cookies, null) { } public FakeControllerContext(IController controller, SessionStateItemCollection sessionItems) : this(controller, null, null, null, null, null, sessionItems) { } public FakeControllerContext(IController controller, NameValueCollection formParams) : this(controller, null, null, formParams, null, null, null) { } public FakeControllerContext(IController controller, NameValueCollection formParams, NameValueCollection queryStringParams) : this(controller, null, null, formParams, queryStringParams, null, null) { } public FakeControllerContext(IController controller, string userName) : this(controller, userName, null, null, null, null, null) { } public FakeControllerContext(IController controller, string userName, string[] roles) : this(controller, userName, roles, null, null, null, null) { } public FakeControllerContext ( IController controller, string userName, string[] roles, NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies, SessionStateItemCollection sessionItems ) : base(new FakeHttpContext(new FakePrincipal(new FakeIdentity(userName), roles), formParams, queryStringParams, cookies, sessionItems), new RouteData(), (Controller)controller) { } }

Unit Testing a Private Method

·1 min
With Visual Studio keep changing the way you create unit tests I thought I’d document down how to test a private method the easy way. Here is a simple HomeController and we want to test that the GetPageSize method return 20 in a unit test.