在ASP.NET MVC中测试HtmlHelpers

时间:2008-11-07 10:21:36

标签: .net asp.net-mvc unit-testing html-helper

有没有办法(单元)测试我自己的HtmlHelpers?如果我想要自定义控件(由HtmlHelper呈现)并且我知道该控件的需求,我怎么能先编写测试 - 然后编写代码?有没有特定的(好的)方法呢?

值得吗?

3 个答案:

答案 0 :(得分:30)

主要问题是你必须模拟HtmlHelper,因为你可能正在使用帮助器的方法来获取路由或值或返回另一个扩展方法的结果。 HtmlHelper类有很多属性,其中一些非常复杂,如ViewContext或当前的Controller。

This post from Ben Hart解释了如何使用Moq创建这样的模拟。可以很容易地翻译成另一个模拟框架。

这是我的Rhino Mocks版本,适应MVC框架的变化。它没有经过全面测试,但它对我有用,但并不期待完美的结果:

    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);
    }

答案 1 :(得分:9)

如果有人正在寻找如何创建HtmlHelper<T>(这就是我所追求的),这里有一个可能有用的实现 - 我的类型是一个名为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);
}

或更通用的实施:

    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);
    }

答案 2 :(得分:0)

我正在创建一个自定义助手,这是我用来模拟使用Moq和ASP MVC 2的httphelper的代码。我也作为参数传递给HttpRequestBase的模拟。如果您不需要,可以删除它

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary viewData, Mock requestMock)
        {
            var contextBaseMock = new Mock();
            contextBaseMock.SetupGet(cb => cb.Request).Returns(requestMock.Object);

            var cc = new ControllerContext(contextBaseMock.Object,
                                            new RouteData(),
                                            new Mock().Object);
            var vctx = new ViewContext(
                cc,
                new Mock().Object,
                viewData,
                new TempDataDictionary(),
                new HtmlTextWriter(new StreamWriter(new MemoryStream())));

            var mockViewDataContainer = new Mock();

            mockViewDataContainer.SetupGet(v => v.ViewData).Returns(viewData);

            return new HtmlHelper(vctx, mockViewDataContainer.Object);
        }