Question

In ASP.NET MVC, I can get information on unit testing for routes and custom routes, but I can not figure out how to do unit testing for IgnoreRoute.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

Practical code is much appreciated.

ASP.NET MVC Framework (Part 2): URL Routing

ASP.NET MVC Tip #13 – Unit Test Your Custom Routes

ASP.NET MVC Tip #30 – Create Custom Route Constraints

Was it helpful?

Solution

I would check that the RouteHandler on the RouteData for a route matching the ignored path is of type StopRoutingHandler;

    [TestMethod]
    public void TestIgnoredRoute()
    {
        // Arrange
        var routes = new RouteCollection();
        GlobalApplication.RegisterRoutes(routes);

        // Act
        var context = new FakeHttpContext("~/some.axd/path");
        var routeData = routes.GetRouteData(context);

        // Assert
        Assert.IsInstanceOfType( routeData.RouteHandler, typeof(StopRoutingHandler) );
    }

OTHER TIPS

If you use the MvcContrib testhelper class (http://nuget.org/packages/MvcContrib.Mvc3.TestHelper-ci), you can simpifly it even further:

[TestMethod]
public void TestIgnoredRoute()
{
    // Arrange
    RouteTable.Routes.Clear();

    // Act
    GlobalApplication.RegisterRoutes(RouteTable.Routes);

    // Assert
    "~/some.axd/path".ShouldBeIgnored();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top