Routing testing in ASP.NET MVC 2.0, a quick workaround
Areas in ASP.NET MVC is a long awaited feature (although there were some workarounds available for version 1.0). If you want to read a quick walkthrough in dividing your ASP.NET MVC project into areas, here is one from msdn.
But, after divided a project into areas, my route unit tests was failing with “System.Web.HttpException: The type initializer for ‘System.Web.Compilation.CompilationLock’ threw an exception”, in my test fixture constructor, where I call my global HttpApplication RegisterRoutes method like this:
RouteCollection routes = new RouteCollection(); Global.RegisterRoutes(routes);
Assuming I have an AreaRegistration object named Routes in “Admin” area and I want its routes tested, that simple workaround did it:
//Global.RegisterRoutes(routes); (new Routes()).RegisterArea( new AreaRegistrationContext("Admin", routes));
Now the route collection is filled with routes from Admin area, ready for tests.
Of course, there are some MVCContrib fluent tests options for routes, very good if you’re using NUnit (not the case here, with all tests using MS Test).