One of the core design principles of the ASP.NET MVC
Framework is enabling great testing support. Like the rest of the MVC
framework, you can easily unit test routes and route matching rules. The
MVC Routing system can be instantiated and run independent of ASP.NET - which
means you can load and unit test route patterns within any unit test library
(no need to start up a web-server) and using any unit test framework (NUnit,
MBUnit, MSTest, etc).
Although you can unit test an ASP.NET MVC Application's
global RouteTable mapping collection directly within your unit tests, in
general it is usually a bad idea to have unit tests ever change or rely on
global state. A better pattern that you can use is to structure your
route registration logic into a RegisterRoutes() helper method like below that
works against a RouteCollection that is passed in as an argument (note: we will
probably make this the default VS template pattern with the next preview
update):
Figure 25
You can then write unit tests that create
their own RouteCollection instance and call the Application's RegisterRoutes()
helper to register the application's route rules within it. You can then
simulate requests to the application and verify that the correct controller and
actions are registered for them - without having to worry about any
side-effects:
Figure 26