How to Unit Test HtmlHelper with Moq?

前端 未结 4 1729
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 20:06

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

4条回答
  •  遇见更好的自我
    2021-02-03 20:48

    Here's another article that shows you how to achieve the same thing:

    public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
    {
      var mockViewContext = new Mock(
        new ControllerContext(
          new Mock().Object,
          new RouteData(),
          new Mock().Object),
        new Mock().Object,
        vd,
        new TempDataDictionary());
    
      var mockViewDataContainer = new Mock();
      mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);
    
      return new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object);
    }
    

提交回复
热议问题