MvcContrib TestHelper在使用AssertViewRendered时发出奇怪的错误

Ali*_*li 7 c# mvccontrib-testhelper asp.net-mvc-3

我正在尝试使用MvcContrib Test Helper来测试MVC3中的控制器方法.

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

考试:

[TestMethod]
public void Index()
{
    // Arrange
    HomeController controller = new HomeController();

    // Act
    ViewResult result = controller.Index() as ViewResult;

    // Assert
    result.AssertViewRendered().ForView("Index");
}
Run Code Online (Sandbox Code Playgroud)

错误:

测试方法Tests.Web.Controllers.HomeControllerTests.Index抛出异常:MvcContrib.TestHelper.ActionResultAssertionException:预期结果为ViewResult类型.它实际上是ViewResult类型.

有任何想法吗?

Kev*_*che 7

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> 
Run Code Online (Sandbox Code Playgroud)

上面的示例指出要求MVC版本1-3使用4的所有程序集.


Dav*_*emp 3

我的猜测是您正在使用 MVC2 的 MVCContrib,并且它使用 MVC2 ViewResult。然而,您返回的是 MVC3 ViewResult。

您是否尝试过针对 MVC3 编译 MVCContrib?