这是MvcContrib TestHelper OutBoundUrl类中的错误吗?

时间:2012-01-25 19:22:15

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

我正在尝试使用MvcContrib TestHelper的静态OutBoundUrl类测试MVC区域中的操作方法的出站URL。这就是我所拥有的:

OutBoundUrl.Of<ErrorsController>(action => action.NotFound())
    .ShouldMapToUrl("/errors/404");

我进入源代码,发现UrlHelper为出站网址返回null。基本上它会创建一个新的UrlHelper并将任务委托给UrlHelper.Action(action, controller, routeValues)

调用时,routeValues不包含任何内容(action方法不带args)。这是一个错误吗?或者我是否需要做一些额外的工作来使这个区域工作?以下行出现在单元测试中的上一行之前,因此我的路由已注册(UrlHelper.RouteCollection包含100多条路由)。

"~/errors/404".WithMethod(HttpVerbs.Get)
    .ShouldMapTo<ErrorsController>(action => action.NotFound());

路线未命名,我不想命名,所以请不要建议我使用OfRouteNamed方法。

更新

我做了一个可以工作的扩展方法,除了一件事。 OutBoundController Of<TController>方法包含以下代码:

var methodCall = ((MethodCallExpression)action.Body);
var methodName = methodCall.Method.Name;

RouteTestingExtensions ShouldMapTo<TController>方法中有类似但严重不同的代码:

var methodCall = (MethodCallExpression) action.Body;
// this line is not important
string expectedAction = methodCall.Method.ActionName();

如果您使用ActionName(),则ActionNameAttributes扩展方法非常棒:

/// <summary>
/// Will return the name of the action specified in the ActionNameAttribute 
/// for a method if it has an ActionNameAttribute.
/// Will return the name of the method otherwise.
/// </summary>
/// <param name="method"></param>
/// <returns></returns>
public static string ActionName(this MethodInfo method)
{
    if (method.IsDecoratedWith<ActionNameAttribute>()) 
        return method.GetAttribute<ActionNameAttribute>().Name;

    return method.Name;
}

回到问题......这是设计,如果是这样,为什么? OutBoundUrl不使用.ActionName()扩展程序这一事实使我无法编写扩展方法来为使用[ActionName]修饰的区域操作生成出站网址。

0 个答案:

没有答案