使用 AssertViewRendered 时 MvcContrib TestHelper 给出奇怪的错误
我正在尝试使用 MvcContrib 测试助手来测试 MVC3 中的控制器方法。
控制器:
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");
}
错误:
测试方法 Tests.Web.Controllers.HomeControllerTests.Index 抛出异常: MvcContrib.TestHelper.ActionResultAssertionException: 预期结果为 ViewResult 类型。它实际上是ViewResult类型。
有什么想法吗?
I am trying to use the MvcContrib Test Helper to test a controller method in MVC3.
The controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
The test:
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
result.AssertViewRendered().ForView("Index");
}
The error:
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.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MVCContrib.TestHelper 使用旧版本的 MVC。该网站现在确实有 MVC3 版本,但在我写这篇文章时,MVC4 已经发布,并且 MVC4 的更新 MVCContrib.TestHelpers 尚不存在。
您可以通过绑定重定向来修复此问题,而无需触及源代码。将其放入您的测试 app.config 中:
上面的示例指出所有要求 MVC 版本 1-3 的程序集都使用 4。
MVCContrib.TestHelper is using an older version of MVC. The site does have an MVC3 version now but as I'm writing this MVC4 is out and an updated MVCContrib.TestHelpers for MVC4 doesn't exist yet.
Without touching the source you can fix this with a binding redirect. Place this in your test app.config:
The above sample points all assemblies asking for MVC version 1-3 to use 4.
我的猜测是您正在使用 MVC2 的 MVCContrib,并且它使用 MVC2 ViewResult。然而,您返回的是 MVC3 ViewResult。
您是否尝试过针对 MVC3 编译 MVCContrib?
My Guess is that you're using the MVCContrib for MVC2, and it uses the MVC2 ViewResult. Whereas, you're returning an MVC3 ViewResult.
Have you tried compiling MVCContrib against MVC3?
万一有人在 2012 年遇到同样的错误,我在 MVC4 和 MvcContrib 与 MVC3 一起工作时遇到了同样的问题。
解决方案是下载 MvcContrib 的源代码。在 MVCContrib.TestHelper 项目中,删除对 System.Web.Mvc 的引用(默认情况下它指向版本 3)并添加 System.Web.Mvc,但请确保引用版本 4.0.0。
然后重建项目,使用 pdb(用于单步执行 TestHelper 代码)将生成的 dll 文件复制到您的解决方案中,并添加对该 dll 的引用。为我工作!
In case somebody comes across the same error in 2012, I'm having the same issue with MVC4 and MvcContrib working against MVC3.
The solution was to download the source code for MvcContrib. In MVCContrib.TestHelper project remove reference to System.Web.Mvc (by default it points to version 3) and add System.Web.Mvc, but make sure you reference version 4.0.0.
Then rebuild the project, copy generated dll files with pdb (for stepping into TestHelper code) into your solution and add reference to that dll. Worked for me!