How to test a PARTIAL view was shown in C # ASP.NET MVC

I have a view, and it has a partial view of rendering inside:

<div class="partialViewDiv">
    @Html.RenderPartial("partial", Model.SomeModelProperty);
</div>

And the controller that returns this view

public ActionResult Action()
        {
            ...
            var model = new SomeModel(){SomeModelProperty = "SomeValue"}
            return View("view", model);
        }

How to check the visualization of a view, I know:

[TestMethod]
public void TestView()
{
   ...
   var result = controller.Action();

   // Assert
   result.AssertViewRendered().ForView("view").WithViewData<SomeModel>();
}

but when i call

result.AssertPartialViewRendered().ForView("partial").WithViewData<SomeModelPropertyType>();

I get this error message

Expected result to be of type PartialViewResult. It is actually of type ViewResult.

What am I doing wrong?

+5
source share
2 answers

What am I doing wrong?

You are testing a controller: such tests essentially mock a view and simply verify that the controller returns the expected view (and model).

Since the view "View", which displays a partial "PartialView", is not involved in testing, so you can not check whether it does what you expect.

, unit test Views; , google "MVC unit test view"

+3

return View(model); 

To

return PartialView(model);

. , .

+2

All Articles