我有默认的路线设置:
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"]);
}
}
我希望这两个断言路由都能成功,但第二个断言路由失败(它路由到controller=“ettan”,而不是controller=“artikel”)。
但是,如果我运行MVC应用程序并浏览到localhost/artikel/123,我将被路由到artikel控制器。这是为什么?我的测试有问题吗?