【问题标题】:MvcContrib TestHelper giving an odd error when using AssertViewRendered使用 AssertViewRendered 时 MvcContrib TestHelper 给出一个奇怪的错误
【发布时间】:2013-04-29 05:15:46
【问题描述】:

我正在尝试使用 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 类型。

有什么想法吗?

【问题讨论】:

    标签: c# asp.net-mvc-3 mvccontrib-testhelper


    【解决方案1】:

    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 项目。
    • 我对 VS12 有相同的结果。我不得不使用较新的 mvc dll 重新编译测试助手。以上在VS10中确实有效。不知道为什么它不在 VS12 中。
    【解决方案2】:

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

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

    【讨论】:

    • @Ali:那你打算怎么做?回到 MVC 2?我还下载了 MVC 3 RC 2,我得到了同样的错误。 MVC contrib 是否与 MVC 3 兼容?
    • 不确定是否诚实。这是针对个人项目的,我正在尝试使用 TDD 方法。我只是暂时不打算使用 MVC contrib Test helper。
    • @Brendan - 您是否尝试过针对 MVC3 RC2 编译 MVCContrib?我不知道 MVC2 和 MVC3 之间发生了什么变化/损坏,但如果你可以让 MVCContrib 针对 MVC3 RC2 程序集进行编译,那么它应该可以解决问题
    【解决方案3】:

    如果有人在 2012 年遇到同样的错误,我在 MVC4 和 MvcContrib 与 MVC3 上工作时遇到了同样的问题。

    解决方案是下载 MvcContrib 的源代码。在 MVCContrib.TestHelper 项目中删除对 System.Web.Mvc 的引用(默认指向版本 3)并添加 System.Web.Mvc,但请确保引用版本 4.0.0。

    然后重建项目,将生成的带有 pdb 的 dll 文件(用于单步执行 TestHelper 代码)复制到您的解决方案中,并添加对该 dll 的引用。为我工作!

    【讨论】:

    • 这对我有用。添加绑定重定向在 VS2012 中似乎没有什么区别......
    • 我最终用 FluentMVCTesting 替换了 TestHelper。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2021-11-16
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多