Search code examples
unit-testingasp.net-mvc-4integration-testingmvccontrib-testhelper

unit/integration testing of controllers and views using MvcContrib throws error when run


Am fairly new to the world of testing MVC 4 Web Applications and have been attempting to unit test views and controllers to see whether for a given controller that an action renders a particular view, I have been using MvcContrib TestHelper to try to simply the process of testing the application but so far have been unable to get the test to pass.

When the test is run I receive the error Expected view name was 'index' actual was ''

Currently I am running this test method:

[TestMethod]
    public void AMAC_Controller_Renders_Index_View()
    {
        var builder = new TestControllerBuilder();
        var controller = new AMACController();
        builder.InitializeController(controller);

        var result = controller.Index();

        result.AssertViewRendered().ForView("index").WithViewData<AMACEnquiryModel>();
    }

the controller and model are both currently in use by the application, was wondering if you could give any advice on how get this test working, I have modified previously when I have done this I get another error that the route name already exists in the collection.


Solution

  • After getting some advice from one of the contributors of the MvcContrib project the reason the test wasn't passing was because I was passing the wrong data into the .ForView(), before I had .ForView("index") where the controller was actually passing View(model) so the value for .ForView() was actually an empty string so the assert now looks like this:

    result.AssertViewRendered().ForView("").WithViewData<AMACEnquiryModel>();