This project is read-only.

How to verify in unit test if partial view is included?

Sep 2, 2014 at 1:12 PM
Edited Sep 2, 2014 at 1:13 PM
Hi,

how can I verify if a partial view is included into rendered html in unit test?

E.g. I have something like this in my view:
@if (someCondition)
{
    @Html.Partial("_PartialSomething", Model);
}
This generates something like "<placeholder>75fb18c8-ec6f-4525-92ac-36d2adec78f0</placeholder>" into html, how can I verify that this is indeed "_PartialSomething" partial view?

Best reagrds,
Kaupo
Sep 2, 2014 at 6:01 PM
If you use RenderAsHtml, the HtmlDocument instance that is generated has strongly typed instances of ViewNode. You can traverse the DOM tree to look for these nodes and verify if the partial exists with the right ViewData.
Marked as answer by kaupov on 9/3/2014 at 1:49 AM
Sep 3, 2014 at 9:48 AM
Thank you pranavkm.

For a reference for others who may struggle with the same problem, here is the code snippet I used:
private static bool ExistsPartialView(HtmlDocument document, string partialViewName)
{
    return
        document.DocumentNode.Descendants("partial")
            .OfType<ViewNode>()
            .Any(descedant => descedant.ViewName.Equals(partialViewName));
 }
Marked as answer by kaupov on 9/3/2014 at 1:49 AM