Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvcContrib TestHelper giving an odd error when using AssertViewRendered

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?

like image 202
Ali Avatar asked Dec 10 '10 14:12

Ali


3 Answers

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.

like image 172
Kevin LaBranche Avatar answered Nov 16 '22 00:11

Kevin LaBranche


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?

like image 24
kͩeͣmͮpͥ ͩ Avatar answered Nov 16 '22 00:11

kͩeͣmͮpͥ ͩ


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!

like image 26
trailmax Avatar answered Nov 16 '22 01:11

trailmax