4

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

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

更新

刚刚试过这个,它似乎工作。这是正确的方法吗?

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

2 回答 2

1

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

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

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

于 2012-03-10T14:53:19.473 回答
0

我一直在寻找同样的东西。我最终添加了以下扩展方法来实现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);
        }
    }
于 2013-01-03T10:21:10.073 回答