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

时间:2012-01-25 17:38:03

标签: asp.net-mvc unit-testing routes mvccontrib-testhelper

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

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

更新

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

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

2 个答案:

答案 0 :(得分:1)

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

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

在幕后,这断言路由由Stop​​RoutingHandler处理。

答案 1 :(得分: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);
        }
    }