为什么我的路由测试失败,但运行应用程序时路由有效?

发布于 2024-09-26 00:37:20 字数 1706 浏览 2 评论 0原文

我已经设置了默认路由:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2024-10-03 00:37:20

AssertRoute 中,您将 httpContext.Request.AppRelativeCurrentExecutionFilePath 设置为始终返回 "~/" 而不是您要测试的 url。

In AssertRoute, you are setting up httpContext.Request.AppRelativeCurrentExecutionFilePath to always return "~/" instead of the url you want to test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文