Thursday, 25 November 2010

Testing routes with MVC3 RC1 and .NET 4

Yesterday I found myself needing to test some MVC routes. I wanted to avoid setting up all the mocking infrastructure needed to do so, and decided to use the MVCContrib project to ease the work. However, I am building an MVC 3 RC1 app on .NET 4 - and MVCContrib targets .NET 3.5 and MVC 2, so I quickly ran into trouble.

I tried converting the solution to .NET 4.0 - but it was too time consuming to do so. Instead I ripped out the pieces I needed, i.e. the route testing extension methods, and built a new assembly targeting the right versions of the framework. And I replaced RhinoMocks with Moq.

Using the Mvc.TestHelper assembly you can easily test routes with syntax like this:
"~/".ShouldMapTo<HomeController>(x => x.Index());
"~/Account/".ShouldMapTo<AccountController>(x => x.Register(null));

Note: I take zero credit for this work. All the source code is taken directly from the MVCContrib project.

I have made the source available on BitBucket so that you can test your MVC 3 routes. The project uses NUnit and Moq for testing.


Unknown said...

I was looking for something precisely like this. I took your route testing source and dropped NUnit for the MSTest assembly, since my current project is MSTest-based. I rebuilt and it's working great testing my MVC3 RC2 routes.

Thanks for sharing the source!

Øyvind Valland said...

Hi Joe,

I'm really happy this was useful to you. The credit really goes to the MCVContrib guys, though, as that's where I got this code from.

Thanks for leaving your comment.


Anonymous said...

Source is no longer available :(

Øyvind Valland said...

The link to the source code has been updated....