Could somebody show me how you would go about creating a mock HTML Helper with Moq?
This article has a link to an article claiming to describe this, but following the li
In MVC5, the ViewContext
has an extra constructor parameter for a TextWriter
, so Thomas' code no longer works. I added an in-memory TextWriter to get around this problem:
public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
Mock mockViewContext = new Mock(
new ControllerContext(
new Mock().Object,
new RouteData(),
new Mock().Object
),
new Mock().Object,
vd,
new TempDataDictionary(),
new StreamWriter(new MemoryStream())
);
Mock mockDataContainer = new Mock();
mockDataContainer.Setup(c => c.ViewData).Returns(vd);
return new HtmlHelper(mockViewContext.Object, mockDataContainer.Object);
}