How do unit testing for IgnoreRoute in ASP.NET MVC

元气小坏坏 提交于 2019-12-21 03:59:07

问题


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


回答1:


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) );
    }



回答2:


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();
}


来源:https://stackoverflow.com/questions/805028/how-do-unit-testing-for-ignoreroute-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!