Testing Routes in ASP.NET MVC with MvcContrib
- by Guilherme Cardoso
I've decide to write about unit testing in the next weeks.
If we decide to develop with Test-Driven Developement pattern, it's important to not forget the routes.
This article shows how to test routes. I'm importing my routes from my RegisterRoutes method from the Global.asax of Project.Web created by default (in SetUp).
I'm using ShouldMapTp() from MvcContrib: http://mvccontrib.codeplex.com/
The controller is specified in the ShouldMapTo() signature, and we use lambda expressions for the action and parameters that are passed to that controller.
[SetUp]
public void Setup()
{
Project.Web.MvcApplication.RegisterRoutes(RouteTable.Routes);
}
[Test]
public void Should_Route_HomeController()
{
"~/Home"
.ShouldMapTo<HomeController>(action => action.Index());
}
[Test]
public void Should_Route_EventsController()
{
"~/Events"
.ShouldMapTo<EventsController>(action => action.Index());
"~/Events/View/44/Concert-DevaMatri-22-January-"
.ShouldMapTo<EventosController>(action => action.Read(1, "Title")); // In this example,44 is the Id for my Event and "Concert-DevaMatri-22-January" is the title for that Event
}
[TearDown]
public void teardown()
{
RouteTable.Routes.Clear();
}