代码之家  ›  专栏  ›  技术社区  ›  MedicineMan

尝试用Moq模拟HtmlHelper时引发MissingMethodException

  •  3
  • MedicineMan  · 技术社区  · 14 年前

    article on mocking the htmlhelper with Moq 我遇到了以下问题。在创建htmlhelper时抛出异常。我只是猜测温莎城堡正在被使用(通过看到错误消息)。

    发生MissingMethodException

    '类型上的构造函数'Castle.Proxies.ViewContextProxy“找不到。

    堆栈跟踪:

    在System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr、Binder Binder、Object[]args、CultureInfo culture、Object[]activationAttributes)

    代码:

        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());
    
            Mock<IViewDataContainer> mockViewDataContainer = new Mock<IViewDataContainer>();
            mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);
    
            return new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object);
        }
    

    如何解决问题并返回HtmlHelper实例?

    2 回复  |  直到 4 年前
        1
  •  2
  •   anon    10 年前

    我已经用我的博客文章中的代码复制了你的问题。以下更新的方法适用于我:

    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 Mock<TextWriter>().Object);
    
        mockViewContext.Setup(vc => vc.ViewData).Returns(vd);
    
        var mockViewDataContainer = new Mock<IViewDataContainer>();
        mockViewDataContainer.Setup(v => v.ViewData)
            .Returns(vd);
    
        return new HtmlHelper(mockViewContext.Object,
                                mockViewDataContainer.Object);
    }
    

    我已经贴了一张 update on my blog 也。

        2
  •  7
  •   Igor Zevaka    14 年前

    你得到了吗 MissingMethodException Mock<>()

    ViewContext 解释了原因。只有一个构造函数重载,它是:

     public ViewContext(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer);
    

    此设置(包括缺少的 TextWriter 模拟)应该工作:

       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 Mock<TextWriter>().Object);