【问题标题】:How do unit testing for IgnoreRoute in ASP.NET MVC如何在 ASP.NET MVC 中对 IgnoreRoute 进行单元测试
【发布时间】:2010-10-22 17:54:11
【问题描述】:
【问题讨论】:
标签:
asp.net-mvc
unit-testing
routes
【解决方案1】:
我会检查 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) );
}
【解决方案2】:
如果您使用 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();
}