
Unit test is a very important part of modern software development. When testing web applications, developers try to cover all aspects of the application from receiving a request to issuing a result. When developing projects using ASP.NET MVC, one of these aspects is the routing mechanism. Testing routes usually brought a lot of headaches, forcing the developer to write a lot of accompanying code. In this post I will show how using a couple of tools you can simplify testing routes to a minimum.
Instruments
First we need the following tools:
- MvcContrib - a library with additional functionality for ASP.NET MVC, including in the field of unit testing;
- RhinoMocks is a library for creating mock objects that simplify testing;
- NUnit is a unit testing environment with a set of its own attributes and tools.
Installation
Install NUnit, then add to your project links to nunit.framework assemblies (from the GAC), Rhino.Mocks and MvcContrib.TestHelper.

The environment is ready to create unit tests for routes.
')
Tests
In order to show examples of unit tests for routes, let's define a couple of such routes:
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
routes.MapRoute(
"Product" ,
"Product/{id}" ,
new { controller = "Product" , action = "GetById" }
);
routes.MapRoute( "ProductList" ,
"ProductList/{year}" ,
new { controller = "Product" , action = "List" },
new { year = new YearConstraint() }
);
routes.MapRoute(
"Default" ,
"{controller}/{action}/{id}" ,
new { controller = "Home" , action = "Index" , id = "" }
);
* This source code was highlighted with Source Code Highlighter .
Pay attention to the ProductList route. It contains a restriction of YearConstraint, the code of which I will skip, but I will only say that this restriction checks the year parameter to belong to the range from 1900 to 2100.For testing, create the following class:namespace Routing
{
using MvcContrib.TestHelper;
using NUnit.Framework;
using Routing.Controllers;
using System.Web.Routing;
using Rhino.Mocks;
using System.Web;
[TestFixture]
public class TestRoutes
{
[TestFixtureSetUp]
public void SetUp()
{
MvcApplication.RegisterRoutes(RouteTable.Routes);
}
[TestFixtureTearDown]
public void TearDown()
{
RouteTable.Routes.Clear();
}
}
}
* This source code was highlighted with Source Code Highlighter .
For those who have worked with NUnit, the attributes will be familiar before, the rest will tell you a little about their purpose:- TestFixture - defines a class with tests;
- TestFixtureSetUp - determines the method with the code that must be run before running the tests;
- TestFixtureTearDown - defines the method with the code that must be run after running the tests.
As you can see, a method has been created in the class that initializes the route table before testing and clears it after testing. Time to write the first test:[Test]
public void TestSimpleRoute()
{
"~/" .Route().ShouldMapTo<HomeController>(x => x.Index());
}
* This source code was highlighted with Source Code Highlighter .
Here, using the extension methods of the MvcContrib library, we check for performance the request to the root of the project, which, according to the routes we have defined, should be routed using the Default route. This route is created in ASP.NET MVC applications by default. A request to the project root by default should be routed to the Index action of the HomeController controller.Compile the project and run NUnit. Having selected our project with the test as a project, click Run to run the test. The result on the picture below:
Test passed. Add the rest of the tests:[Test]
public void TestProduct()
{
"~/Product/750" .Route().ShouldMapTo<ProductController>(x => x.GetById(750));
}
[Test]
public void TestProductListValidYear()
{
"~/ProductList/2009" .Route().ShouldMapTo<ProductController>(x => x. List (2009));
}
[Test]
public void TestProductListInvalidYear()
{
Assert.AreNotEqual( "~/ProductList/1800" .Route().Values[ "controller" ], "Product" );
}
[Test]
public void TestIgnoreAxd()
{
"~/someroutetoigonre.axd" .ShouldBeIgnored();
}
* This source code was highlighted with Source Code Highlighter .
Here are four tests:
- TestProduct - designed to test the Product route;
- TestProductListValidYear - test to test the route with the restriction when the year parameter is correctly set;
- TestProductListInvalidYear - test to test the route with the restriction when the year parameter is incorrectly specified;
- TestIgnoreAxd - test for testing the route of ignoring requests to axd-resources.
Pay attention to TestProductListInvalidYear. In this test, we check the routing operation when trying to access the PriductList route with the wrong parameter. The check is somewhat indirect: we make sure that if the parameter is incorrect, the routing mechanism will not cause the Product controller.
And the last thing you should pay attention to is how easy it is to create a test for checking ignore routes. The extension method ShouldBeIgnored allows you to set a test for such routes. In our test, TestIgnoreAxd, we try to present a situation with access to an axd-resource, which, according to the specified routes, should be ignored.
Compile and run the tests in NUnit:

All tests passed. The work is done.
Conclusion
In this article, I looked at the simplest test cases for unit testing routes in ASP.NET MVC. The tools that are represented in the MvcContrib library make it very easy to create such tests and, of course, make life easier for the developer.
For your projects, you will probably want to create more detailed unit tests of the routing mechanism. This will help you MvcContrib. Program with pleasure!
