MvcContrib TestHelper使用AssertViewRendered时出现奇怪的错误
我想使用MvcContrib测试助手来测试MVC3中的控制器方法。MvcContrib TestHelper使用AssertViewRendered时出现奇怪的错误
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
测试:
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
result.AssertViewRendered().ForView("Index");
}
错误:
Test method Tests.Web.Controllers.HomeControllerTests.Index threw exception: MvcContrib.TestHelper.ActionResultAssertionException: Expected result to be of type ViewResult. It is actually of type ViewResult.
任何想法?
我的猜测是你正在使用MVCContrib for MVC2,它使用MVC2 ViewResult。而你正在返回一个MVC3 ViewResult。
你试过编译MVCContrib对MVC3吗?
如果在2012年有人遇到同样的错误,我遇到与MVC4和MvcContrib对MVC3工作相同的问题。
解决方案是下载MvcContrib的源代码。在MVCContrib.TestHelper项目中删除对System.Web.Mvc的引用(默认情况下它指向版本3)并添加System.Web.Mvc,但请确保您引用版本4.0.0。
然后重建项目,复制生成的dll文件与pdb(用于步入TestHelper代码)到您的解决方案并添加对该dll的引用。为我工作!
MVCContrib.TestHelper使用的是较旧版本的MVC。该网站现在确实有一个MVC3版本,但是在我写这篇MVC4的时候,MVC4的更新MVCContrib.TestHelpers还不存在。
如果不触及源代码,您可以使用绑定重定向来修复此问题。将此放在您的测试的app.config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
以上的采样点要求MVC版本1-3全部组件使用4
我刚刚在VS2012的我的测试项目中添加了MvcContrib.Mvc3.TestHelper-ci包。我在测试项目中添加了一个'app.config'文件,并且在这个过程中的某处添加了上述绑定重定向到您的app.config中(我认为它是NuGet)。无论如何,这并不能解决问题。我仍然得到了“...预期类型'ViewResult',但实际上是类型'ViewResult'”问题。我在VS2012中的解决方案是@trailmax声明...用ASP.Net MVC 4重新编译MvcContrib.TestHelper项目。 – JTech 2012-12-20 21:18:38
我对VS12有同样的结果。我不得不使用新的mvc dll重新编译测试助手。上面的工作在VS10中。不知道为什么它不在VS12中。 – klabranche 2012-12-21 14:00:32
刚试过用MVC2项目一样,所有工作 – Ali 2010-12-10 15:23:07
@阿里:那你打算怎么做?回到MVC 2?我也下载了MVC 3 RC 2,并且遇到了同样的错误。 MVC contrib与MVC 3兼容吗? – 2010-12-11 10:47:13
不确定是否诚实。这是一个个人项目,我正在尝试使用TDD方法。现在我只是不打算使用MVC contrib测试助手。 – Ali 2010-12-13 11:21:22