为什么我的路由测试失败,但运行应用程序时路由有效?
我已经设置了默认路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Shortie", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Ettan", action = "Index", id = "id" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Ettan", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
我正在 nunit/resharper 中运行以下测试,使用 Moq 来设置 HttpContext:
[TestFixture]
public class RoutesTests
{
[Test]
public void RoutingTest()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
AssertRoute(routes, "~/", "Ettan", "Index");
AssertRoute(routes, "~/Artikel/123", "Artikel", "Index");
}
public static void AssertRoute(RouteCollection routes, string url, string controller, string action)
{
var httpContext = new Mock<HttpContextBase>();
httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
var routeData = routes.GetRouteData(httpContext.Object);
Assert.IsNotNull(routeData);
Assert.AreEqual(controller, routeData.Values["Controller"]);
Assert.AreEqual(action, routeData.Values["Action"]);
}
}
我期望两个 AssertRoutes 都能成功,但第二个失败(它路由到controller =) “Ettan”,而不是控制器=“Artikel”)。
但是,如果我运行 mvc 应用程序并浏览到 localhost/Artikel/123,我将被路由到 Artikel 控制器。这是为什么呢?我的测试有问题吗?
I've got the default route set-up:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Shortie", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Ettan", action = "Index", id = "id" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Ettan", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I'm running the following test in nunit/resharper, using Moq to setup the HttpContext:
[TestFixture]
public class RoutesTests
{
[Test]
public void RoutingTest()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
AssertRoute(routes, "~/", "Ettan", "Index");
AssertRoute(routes, "~/Artikel/123", "Artikel", "Index");
}
public static void AssertRoute(RouteCollection routes, string url, string controller, string action)
{
var httpContext = new Mock<HttpContextBase>();
httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
var routeData = routes.GetRouteData(httpContext.Object);
Assert.IsNotNull(routeData);
Assert.AreEqual(controller, routeData.Values["Controller"]);
Assert.AreEqual(action, routeData.Values["Action"]);
}
}
I'm expecting both AssertRoutes to success, but the second one fails (it routes to controller = "Ettan", not controller = "Artikel").
However, if I run the mvc application and browse to localhost/Artikel/123, I will be routed to the Artikel controller. Why is this? Something wrong with my tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
AssertRoute
中,您将httpContext.Request.AppRelativeCurrentExecutionFilePath
设置为始终返回"~/"
而不是您要测试的 url。In
AssertRoute
, you are setting uphttpContext.Request.AppRelativeCurrentExecutionFilePath
to always return"~/"
instead of the url you want to test.