MvcContrib TestHelper giving an odd error when using AssertViewRendered
Asked Answered
P

3

7

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?

Philippi answered 10/12, 2010 at 14:11 Comment(0)
B
3

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?

Bronny answered 10/12, 2010 at 14:21 Comment(3)
@Ali: So what you going to do? Go back to MVC 2? I also downloaded MVC 3 RC 2 and I am getting the same error. Is MVC contrib compatible with MVC 3?Kling
Not sure to be honest. this is for a personal project, and I'm trying to use a TDD approach. I was just not going to use the MVC contrib Test helper for now.Philippi
@Brendan - have you tried compiling MVCContrib against MVC3 RC2? I don't know what's changed/broken between MVC2 and MVC3, but if you can get MVCContrib to compile against the MVC3 RC2 assemblies, then it should fix the problemLemnos
D
7

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:

<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> 

The above sample points all assemblies asking for MVC version 1-3 to use 4.

Draftsman answered 17/9, 2012 at 18:39 Comment(2)
I have just added the MvcContrib.Mvc3.TestHelper-ci package into my Test project in VS2012. I added an 'app.config' file to the Test project and somewhere along the way, something added the above binding redirect in your app.config for me (I think it was NuGet). Anyway, this did not alone manage to fix the problem. I still got the "...expected Type 'ViewResult' but actual was Type 'ViewResult'" issue. The solution for me in VS2012 is as @trailmax stated...recompile the MvcContrib.TestHelper project with ASP.Net MVC 4.Lely
I had the same results for VS12. I had to recompile the test helper using the newer mvc dll. The above does work in VS10. Not sure why it doesn't in VS12.Draftsman
B
3

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?

Bronny answered 10/12, 2010 at 14:21 Comment(3)
@Ali: So what you going to do? Go back to MVC 2? I also downloaded MVC 3 RC 2 and I am getting the same error. Is MVC contrib compatible with MVC 3?Kling
Not sure to be honest. this is for a personal project, and I'm trying to use a TDD approach. I was just not going to use the MVC contrib Test helper for now.Philippi
@Brendan - have you tried compiling MVCContrib against MVC3 RC2? I don't know what's changed/broken between MVC2 and MVC3, but if you can get MVCContrib to compile against the MVC3 RC2 assemblies, then it should fix the problemLemnos
K
1

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!

Kip answered 7/9, 2012 at 21:39 Comment(2)
This is what worked for me. Adding the binding redirect made no difference in VS2012 it seemed...Lely
I ended up replacing TestHelper with FluentMVCTesting.Kip

© 2022 - 2024 — McMap. All rights reserved.