views:

199

answers:

1

I have an HTMLHelper extension method that outputs HTML to Response.Write. How best to unit test this?

I'm considering mocking the HtmlHelper passed into the method, but am unsure as to how I should verify the HTML sent to Response.Write.

Thanks

+4  A: 

If you are using an HTML helper to output text to the browser why not have it return a string and in your view do something like ...

<%=Html.YourExtension() %>

It makes it a great deal more testable.

Kindness,

Dan

EDIT:

Modification would be a change of signature

public static void YourExtension(this HtmlHelper html)
{
   ...
   Response.Write(outputSting);
}

to

public static string YourExtension(this HtmlHelper html)
{
   ...
   return outputSting;
}
Daniel Elliott
Editted with the suggested modification
Daniel Elliott