使用 MvcContrib TestHelper 断言不应映射入站路由

发布于 2024-12-28 23:27:47 字数 340 浏览 1 评论 0原文

在名为 ShouldNotMap 的 MvcContrib.TestHelper.RouteTestingExtensions 类上查找方法。有 ShouldBeIgnored,但我不想测试 IgnoreRoute 调用。我想测试特定的传入路由不应映射到任何资源。

有没有办法使用 MvcContrib TestHelper 来做到这一点?

更新

刚刚尝试过,它似乎有效。这是正确的方法吗?

"~/do/not/map/this".Route().ShouldBeNull();

Looked for a method on the MvcContrib.TestHelper.RouteTestingExtensions class named ShouldNotMap. There is ShouldBeIgnored, but I don't want to test an IgnoreRoute invocation. I want to test that a specific incoming route should not be mapped to any resource.

Is there a way to do this using MvcContrib TestHelper?

Update

Just tried this, and it seems to work. Is this the correct way?

"~/do/not/map/this".Route().ShouldBeNull();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

贪了杯 2025-01-04 23:27:47

我认为您正在寻找以下内容:

"~/do/not/map/this".ShouldBeIgnored();

在幕后,这断言路由是由 StopRoutingHandler 处理的。

I think you are looking for the following:

"~/do/not/map/this".ShouldBeIgnored();

Behind the scenes this asserts that the route is processed by StopRoutingHandler.

自此以后,行同陌路 2025-01-04 23:27:47

我一直在寻找同样的东西。我最终添加了以下扩展方法来实现 ShouldBeNull 和更短的 ShouldNotMap

RouteTestingExtensions.cs 中:

    /// <summary>
    /// Verifies that no corresponding route is defined.
    /// </summary>
    /// <param name="relativeUrl"></param>
    public static void ShouldNotMap(this string relativeUrl)
    {
        RouteData routeData = relativeUrl.Route();
        routeData.ShouldBeNull(string.Format("URL '{0}' shouldn't map.", relativeUrl));
    }

    /// <summary>
    /// Verifies that the <see cref="RouteData">routeData</see> is null.
    /// </summary>
    public static void ShouldNotMap(this RouteData routeData)
    {
        routeData.ShouldBeNull("URL should not map.");
    }

GeneralTestExtensions.cs 中:

    ///<summary>
    /// Asserts that the object should be null.
    ///</summary>
    ///<param name="actual"></param>
    ///<param name="message"></param>
    ///<exception cref="AssertFailedException"></exception>
    public static void ShouldBeNull(this object actual, string message)
    {
        if (actual != null)
        {
            throw new AssertFailedException(message);
        }
    }

I was looking for the same thing. I ended up adding the following extension methods to implement ShouldBeNull and the even shorter ShouldNotMap:

In RouteTestingExtensions.cs:

    /// <summary>
    /// Verifies that no corresponding route is defined.
    /// </summary>
    /// <param name="relativeUrl"></param>
    public static void ShouldNotMap(this string relativeUrl)
    {
        RouteData routeData = relativeUrl.Route();
        routeData.ShouldBeNull(string.Format("URL '{0}' shouldn't map.", relativeUrl));
    }

    /// <summary>
    /// Verifies that the <see cref="RouteData">routeData</see> is null.
    /// </summary>
    public static void ShouldNotMap(this RouteData routeData)
    {
        routeData.ShouldBeNull("URL should not map.");
    }

In GeneralTestExtensions.cs :

    ///<summary>
    /// Asserts that the object should be null.
    ///</summary>
    ///<param name="actual"></param>
    ///<param name="message"></param>
    ///<exception cref="AssertFailedException"></exception>
    public static void ShouldBeNull(this object actual, string message)
    {
        if (actual != null)
        {
            throw new AssertFailedException(message);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文