如何在ASP.NET MVC中对IgnoreRoute进行单元测试

时间:2009-04-30 01:32:44

标签: asp.net-mvc unit-testing routes

在ASP.NET MVC中,我可以获得有关路由和自定义路由的单元测试的信息,但我无法弄清楚如何对IgnoreRoute进行单元测试。

routes.IgnoreRoute( “{}资源个.axd / {*} PATHINFO”);

非常感谢实用代码。

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

2 个答案:

答案 0 :(得分:15)

我会检查RouteData上与被忽略路径匹配的路由的RouteHandler是否类型为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) );
    }

答案 1 :(得分:0)

如果您使用MvcContrib testhelper类(http://nuget.org/packages/MvcContrib.Mvc3.TestHelper-ci),您可以进一步简化:

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

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

    // Assert
    "~/some.axd/path".ShouldBeIgnored();
}