ASP.NET MVC Route testing with MVCContrib Test Helper

ASP.NET MVC,Testing 28 November 2011 | 0 Comments

One tool I’ve found in my aim to have peace of mind about my ASP.NET MVC applications is MVCContrib’s Test Helper extensions. Found here.

It enables you to write unit tests to assert that your routes resolve correctly and to the correct places.

Take for example the following route test, this asserts when someone posts to the url /enquiry that it resolves to the Enquiry Controller and to the Enquiry method. The null parameter in my case is the view model that is to be passed.

[Test]
public void EnquiryPost()
{
	"~/enquiry"
	.WithMethod(HttpVerbs.Post)
	.ShouldMapTo<EnquiryController>(action => action.Enquiry(null));
}

I’ve successfully used this throughout the project during my TDD to help validate that my routing works as I expect. I think this is a very clean unit test that doesn’t take a large amount of thought to follow, my previous attempted involved lots of nasty abstraction and mocking.

Leave a Reply