Simple Single Line ASP.NET MVC Route Testing

Last year I stumbled accross an exciting new ASP.NET MVC Route Testing library that is still relevant. I played with it for a while as I built out my xUnit Template extensions for VS IDE. I quickly found writing route unit tests could be elegant and simple using the fluent api available. The ability to write and read a unit test like english is extremely natural and can be very benefitial for less experience programmers who may have difficulty writing unit tests. The other day I found myself introducing this library into yet another ASP.NET MVC project to provide developers a quick, simple method to unit test their routes. If you test nothing else, test your routes! You will find it very easy to introduce breaking changes as routes are added. Help yourself, prevent route pain, find the route issues prior to testing release by leveraging MVCRouteTester. MVCRouteTester supports ASP.NET MVC 4, 5 and 5.1. Do you prefer elegant, simple unit tests like this – testRoutes.ShouldMap("/").To<HomeController>(c => c.Index());?



Setup a base route class for out test facts to provide simple wrapper for ASP.NET MVC and Web API route tables.

RouteingBaseFact
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class RoutingBaseFact : IDisposable
{
    protected static readonly Guid TestGuid = Guid.NewGuid();
    //protected HttpConfiguration config;
    protected RouteCollection testRoutes;
    public RoutingBaseFact()
    {
        //config = GlobalConfiguration.Configuration;
        testRoutes = RouteTable.Routes;
        //WebApiConfig.Register(config);
        RouteConfig.RegisterRoutes(testRoutes);
    }
    public void Dispose()
    {
        RouteTable.Routes.Clear();
    }
}

Create a RouteFacts test class inheriting from RoutingBaseFact class. Leverage the Arrange Act Assert (AAA) guidance with proper test method naming conventions preached by Roy Osherove. Feast your eyes on the elegant, simple route testing made possible by MVCRouteTester below.

UnitOfWork_Scenario_ExpectedResult

MethodNameToTest_Scenario_ExpectedResult

RouteFacts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[TestFixture]
public class RouteFacts : RoutingBaseFact
{
    // Usage
    // https://github.com/AnthonySteele/MvcRouteTester/wiki/Usage
    //
    [Test]
    public void DefaultRoute_RootWithNoControllerNoActionNoId_ShouldMapToIndex()
    {
        testRoutes.ShouldMap("/").To<HomeController>(c => c.Index());
    }
    [Test]
    public void Home_RouteWithControllerNoActionNoId_ShouldMapToIndex()
    {
        testRoutes.ShouldMap("/home").To<HomeController>(c => c.Index());
    }
    [Test]
    public void Home_RouteWithControllerActionNoId_ShouldMapToIndexWithAction()
    {
        testRoutes.ShouldMap("/home").To<HomeController>(c => c.Index());
    }
    [Test]
    public void HomeIndex_RouteWithControllerIndexActionNoId_ShouldMapToIndex()
    {
        testRoutes.ShouldMap("/home/index").To<HomeController>(c => c.Index());
    }
    [Test]
    public void HomeAbout_RouteWithControllerAboutActionNoId_ShouldMapToAbout()
    {
        testRoutes.ShouldMap("/home/about").To<HomeController>(c => c.About());
    }
    [Test]
    public void HomeContact_RouteWithControllerContactActionNoId_ShouldMapToContact()
    {
        testRoutes.ShouldMap("/home/contact").To<HomeController>(c => c.Contact());
    }
}

Reference

Comments