Why does mvccontrib's AssertViewRendered().ForView("Edit") fail due to view name being full cshtml path?

1.4k views Asked by At

I have the following unit test:

    [TestMethod]
    public void Add_Returns_Edit_View()
    {
        // Act
        ActionResult result = _controller.Add();

        // Verify
        result.AssertViewRendered().ForView("Edit");
    }

This should be passing, since the Add action is returning the Edit view. However, this assertion fails with the following exception

MvcContrib.TestHelper.ActionResultAssertionException: Expected view name 'Edit', actual was '~/Views/JobSearch/Edit.cshtml'

Why is the view name coming back as the full path name? Could this be due to my usage of T4MVC, and if so how can I get this to pass?


Edit The Add view looks like this:

    public virtual ActionResult Add()
    {
        return View(MVC.JobSearch.Views.Edit, new JobSearch());
    }
2

There are 2 answers

0
VinnyG On BEST ANSWER

You can test against the T4MVC value like this :

result.AssertViewRendered().ForView(MVC.JobSearch.Views.Edit);

I think it's the cleaner solution... If you have better let me know :)

0
KallDrexx On

Ok I guess I just didn't investigate into the T4MVC code to get a better look at what is happening. It seems that the MVC.JobSearch.Views.Edit is equal to "~/Views/JobSearch/Edit.cshtml", and so .ForView() is testing against the same exact view name that is described in the View() method call.

Thanks for the help.