2013-06-30 28 views

回答

8

虽然RedirectToRouteResultSystem.Web.Mvc组件被定义,RouteValueDictionarySystem.Web组件定义(SI这是在MVC框架之前引入的机制的一部分)。

尝试在您的测试项目中添加对System.Web的引用,看看是否有帮助。

+0

它没有帮助:/ – michael

+0

你尝试了什么版本的'System.Web'? – haim770

2

添加对System.Web.Routing的引用

0

将System.Web的引用添加到您的测试项目中。它有助于我的情况。

+0

这复制了另一个答案并且没有添加任何新内容。除非你真的有新的东西来贡献,否则请不要发布答案。 – DavidPostill

1

您可能使用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"]); 

注意演员。

0

您只需要在测试项目中添加“System.Web”和“System.Web.Routing”的引用。您可以通过右键单击测试项目中的引用来添加它。

+0

这只是以前答案的复本。请不要在没有新的相关/有价值内容的情况下发布答案 –

相关问题