How do unit testing for IgnoreRoute in ASP.NET MVC

萝らか妹 提交于 2019-12-03 11:23:31

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

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