您可能使用MVC 5 如果是这样的话(这是我的情况),这里是一个解决方案,从Unit Testing Controller Actions in MVC 5(第4部分 - 使用RouteValues字典):
When trying to assert values returned within an action result we utilise the ActionResults
RouteValueDictionar
, e.g.
Assert.AreEqual(“Index”, result.RouteValues[“action”],“Unexpected View Name”);
However, doing so in your MVC 5 unit test project may throw the following compile error: Cannot apply indexing with [] to an expression of type ‘System.Web.Routing.RouteValueDictionary’
Solution: Add System.Web.Routing 4.0 and System.Web 4.0 from the “Add Reference” dialog. Routing is dependent on system.web. Add the System.Web.Routing using declaration to your class file.
你的代码看起来像
var result = (RedirectToRouteResult)controller.Create();
Assert.AreEqual("AddUnits", ((RouteValueDictionary)result.RouteValues)["action"]);
注意演员。
它没有帮助:/ – michael
你尝试了什么版本的'System.Web'? – haim770