I've spent the better half of this afternoon trying to figure out how to mock (using MOQ) up the relevant bits and pieces to unit test the following HtmlHelper, which users the UrlHelper class to create img tags:
public static IHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)
{
// Instantiate a UrlHelper
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
// Create tag builder
var builder = new TagBuilder("img");
// Create valid id
builder.GenerateId(id);
// Add attributes
builder.MergeAttribute("src", urlHelper.Content(url));
builder.MergeAttribute("alt", alternateText);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
// Render tag
return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));
}
Has anyone solved this problem already in their work?