MvcContrib TestHelper の静的OutBoundUrl
クラスを使用して、MVC 領域でアクション メソッドの送信 URL をテストしようとしています。ここに私が持っているものがあります:
OutBoundUrl.Of<ErrorsController>(action => action.NotFound())
.ShouldMapToUrl("/errors/404");
ソースに足を踏み入れたところUrlHelper
、アウトバウンド URL に対して が null を返していることがわかりました。基本的に、新しい UrlHelper を作成し、タスクを に委任しますUrlHelper.Action(action, controller, routeValues)
。
呼び出されると、routeValues
何も含まれません (アクション メソッドは引数を取りません)。これはバグですか?それとも、これをエリアで機能させるために何か特別なことをする必要がありますか? 単体テストでは、次の行が上記の行の直前にあるため、ルートが登録されています (100 以上のUrlHelper.RouteCollection
ルートが含まれています)。
"~/errors/404".WithMethod(HttpVerbs.Get)
.ShouldMapTo<ErrorsController>(action => action.NotFound());
ルートには名前が付けられていません。名前を付けたくないので、このOfRouteNamed
方法を使用することを提案しないでください。
アップデート
1つのことを除いて、機能する拡張メソッドを作成しました。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]