Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing HtmlHelpers in ASP.NET MVC

Tags:

Is there any way to (unit) test my own HtmlHelpers? In case when I'd like to have custom control (rendered by HtmlHelper) and I know requierements for that control how could I write tests first - and then write code? Is there a specific (nice) way to do that?

Is it worth?

like image 936
rafek Avatar asked Nov 07 '08 10:11

rafek


2 Answers

The main problem is that you have to mock the HtmlHelper because you may be using methods of the helper to get routes or values or returning the result of another extension method. The HtmlHelper class has quite a lot of properties and some of them quite complex like the ViewContext or the current Controller.

This post from Ben Hart that explains how to create such a mock with Moq. Can be easily translated to another mock framework.

This is my Rhino Mocks version adapted to the changes in the MVC Framework. It's not fully tested but it's working for me but don't expect perfect results:

    public static HtmlHelper CreateHtmlHelper(ViewDataDictionary viewData)     {         var mocks = new MockRepository();          var cc = mocks.DynamicMock<ControllerContext>(             mocks.DynamicMock<HttpContextBase>(),             new RouteData(),             mocks.DynamicMock<ControllerBase>());          var mockViewContext = mocks.DynamicMock<ViewContext>(             cc,             mocks.DynamicMock<IView>(),             viewData,             new TempDataDictionary());          var mockViewDataContainer = mocks.DynamicMock<IViewDataContainer>();          mockViewDataContainer.Expect(v => v.ViewData).Return(viewData);          return new HtmlHelper(mockViewContext, mockViewDataContainer);     } 
like image 163
Marc Climent Avatar answered Sep 21 '22 15:09

Marc Climent


If anyone is looking for how to create HtmlHelper<T> (that's what I was after), here is an implementation that might help - my type is a class named Model:

public static HtmlHelper<Model> CreateHtmlHelper() {     ViewDataDictionary vd = new ViewDataDictionary(new Model());      var controllerContext = new ControllerContext(new Mock<HttpContextBase>().Object,                                                   new RouteData(),                                                   new Mock<ControllerBase>().Object);      var viewContext = new ViewContext(controllerContext, new Mock<IView>().Object, vd, new TempDataDictionary(), new Mock<TextWriter>().Object);      var mockViewDataContainer = new Mock<IViewDataContainer>();     mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);      return new HtmlHelper<Model>(viewContext, mockViewDataContainer.Object); } 

Or a more generic implementation:

    public HtmlHelper<T> CreateHtmlHelper<T>() where T : new()     {         var vd = new ViewDataDictionary(new T());          var controllerContext = new ControllerContext(new Mock<HttpContextBase>().Object,                                                       new RouteData(),                                                       new Mock<ControllerBase>().Object);          var viewContext = new ViewContext(controllerContext, new Mock<IView>().Object, vd, new TempDataDictionary(), new Mock<TextWriter>().Object);          var mockViewDataContainer = new Mock<IViewDataContainer>();         mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);          return new HtmlHelper<T>(viewContext, mockViewDataContainer.Object);     } 
like image 33
CRice Avatar answered Sep 21 '22 15:09

CRice