如何避免在asp.net-mvc项目中使用Moq模拟HtmlHelper时因缺少方法而引发MissingMethodException异常?

2026-03-30 13:021阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计418个文字,预计阅读时间需要2分钟。

如何避免在asp.net-mvc项目中使用Moq模拟HtmlHelper时因缺少方法而引发MissingMethodException异常?

在尝试使用Moq模拟HtmlHelper时,我遇到了以下问题。创建HtmlHelper实例时抛出了异常。似乎是在使用城堡(Castle)Windsor容器时出现了错误。具体错误信息如下:MissingMethodException: 'Construc'。这表明可能在构造过程中缺少某个方法。我怀疑这是在通过查看错误消息时使用城堡Windsor容器导致的。

当试图遵循 article on mocking the htmlhelper with Moq时,我遇到了以下问题.创建htmlhelper时会抛出异常.我只是猜测正在使用城堡windsor(通过查看错误消息).

如何避免在asp.net-mvc项目中使用Moq模拟HtmlHelper时因缺少方法而引发MissingMethodException异常?

例外:

MissingMethodException occurred

Constructor on type ‘Castle.Proxies.ViewContextProxy’ not found.

堆栈跟踪:

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

我正在使用ASP MVC 2,Moq 4.0 beta 3,VS2010,使用IDE的测试框架.

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

我已经通过我的博客文章中的代码重现了您的问题.以下更新的方法适用于我:

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.

本文共计418个文字,预计阅读时间需要2分钟。

如何避免在asp.net-mvc项目中使用Moq模拟HtmlHelper时因缺少方法而引发MissingMethodException异常?

在尝试使用Moq模拟HtmlHelper时,我遇到了以下问题。创建HtmlHelper实例时抛出了异常。似乎是在使用城堡(Castle)Windsor容器时出现了错误。具体错误信息如下:MissingMethodException: 'Construc'。这表明可能在构造过程中缺少某个方法。我怀疑这是在通过查看错误消息时使用城堡Windsor容器导致的。

当试图遵循 article on mocking the htmlhelper with Moq时,我遇到了以下问题.创建htmlhelper时会抛出异常.我只是猜测正在使用城堡windsor(通过查看错误消息).

如何避免在asp.net-mvc项目中使用Moq模拟HtmlHelper时因缺少方法而引发MissingMethodException异常?

例外:

MissingMethodException occurred

Constructor on type ‘Castle.Proxies.ViewContextProxy’ not found.

堆栈跟踪:

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

我正在使用ASP MVC 2,Moq 4.0 beta 3,VS2010,使用IDE的测试框架.

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

我已经通过我的博客文章中的代码重现了您的问题.以下更新的方法适用于我:

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.