How to Unit Test HtmlHelper with Moq?

前端 未结 4 1721
爱一瞬间的悲伤
爱一瞬间的悲伤 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:47

    To test disposable helper like BeginForm with access to ViewContext.Writer you can use this:

    public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd, Stream stream = null)
    {
        TextWriter textWriter = new StreamWriter(stream ?? new MemoryStream());
        Mock<ViewContext> mockViewContext = new Mock<ViewContext>(
            new ControllerContext(
                new Mock<HttpContextBase>().Object,
                new RouteData(),
                new Mock<ControllerBase>().Object
            ),
            new Mock<IView>().Object,
            vd,
            new TempDataDictionary(),
            textWriter
        );
        mockViewContext.Setup(vc => vc.Writer).Returns(textWriter);
    
        Mock<IViewDataContainer> mockDataContainer = new Mock<IViewDataContainer>();
        mockDataContainer.Setup(c => c.ViewData).Returns(vd);
    
        return new HtmlHelper(mockViewContext.Object, mockDataContainer.Object);
    }
    
    0 讨论(0)
  • 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<ViewContext>(
        new ControllerContext(
          new Mock<HttpContextBase>().Object,
          new RouteData(),
          new Mock<ControllerBase>().Object),
        new Mock<IView>().Object,
        vd,
        new TempDataDictionary());
    
      var mockViewDataContainer = new Mock<IViewDataContainer>();
      mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);
    
      return new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object);
    }
    
    0 讨论(0)
  • 2021-02-03 20:58

    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<ViewContext> mockViewContext = new Mock<ViewContext>(
            new ControllerContext(
                new Mock<HttpContextBase>().Object,
                new RouteData(),
                new Mock<ControllerBase>().Object
            ),
            new Mock<IView>().Object,
            vd,
            new TempDataDictionary(),
            new StreamWriter(new MemoryStream())
        );
    
        Mock<IViewDataContainer> mockDataContainer = new Mock<IViewDataContainer>();
        mockDataContainer.Setup(c => c.ViewData).Returns(vd);
    
        return new HtmlHelper(mockViewContext.Object, mockDataContainer.Object);
    }
    
    0 讨论(0)
  • 2021-02-03 21:04

    What you can do is this:

    HtmlHelper helper = null;
    helper.YourHelperMethod();
    

    No need to mock anything. Works brilliant for me.

    0 讨论(0)
提交回复
热议问题