ASP.NET-MVC – Try to throw MissingMethodeXception when you use MOQ to simulate HTMLHELPER

When trying to follow the article on mocking the htmlhelper with Moq, I encountered the following problem. An exception was thrown when creating the htmlhelper. I just guessed that the castle windsor is being used (by viewing the error message) .

Exception:

MissingMethodException occurred

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

Stack trace:

at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args , CultureInfo culture, Object[] activationAttributes)

Code:

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{< br /> Mock mockViewContext = new Mock(
new ControllerContext(
new Mock().Object,
new RouteData(),
newMock().Object),
new Mock().Object,
vd,
new TempDataDictionary());

Mock< IViewDataContainer> mockViewDataContainer = new Mock();
mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);

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

I am using ASP MVC 2, Moq 4.0 beta 3, VS2010, using IDE’s testing framework.

How to solve the problem and return to HtmlHelper Examples?

I have reproduced your problem with the code in my blog post. The following updated The method works for me:

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
Mock mockViewContext = new Mock (
new ControllerContext(
new Mock().Object,
new RouteData(),
new Mock().Object),
new Mock().Object,
vd,
new TempDataDictionary(),
new Mock().Object);

mockViewContext. Setup(vc => vc.ViewData).Returns(vd);

var mockViewDataContainer = new Mock();
mockViewDataContainer.Setup(v => v.ViewData)< br /> .Returns(vd);

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

I also posted An update on my blog.

When trying to follow When article on mocking the htmlhelper with Moq, I encountered the following problem. An exception was thrown when creating htmlhelper. I just guessed that the castle windsor is being used (by viewing the error message).

Exception:< /p>

MissingMethodException occurred

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

Stack Tracking:

at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

< /blockquote>

Code:

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
Mock mockViewContext = new Mock (
new ControllerContext(
new Mock().Object,
new RouteData(),
new Mock().Object),
new Mock().Object,
vd,
new TempDataDictionary());

Mock mockViewDataContainer = new Mock();
mockViewDataContainer.Setup(v => v.ViewData).Returns(vd);

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

< p>I’m using ASP MVC 2, Moq 4.0 beta 3, VS2010, using the IDE’s testing framework.

How to solve the problem and return an instance of HtmlHelper?

I have reproduced your problem with the code in my blog post. The following updated method works for me:

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd)
{
Mock mockViewContext = new Mock(
new ControllerContext(
new Mock().Object,
new RouteData(),
new Mock().Object),
new Mock().Object,
vd,
new TempDataDictionary(),
new Mock().Object);

mockViewContext.Setup(vc => vc.ViewData).Returns( vd);

var mockViewDataContainer = new Mock();
mockViewDataContainer.Setup(v => v.ViewData)
.Returns(vd);

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

I also posted an update on my blog.

Leave a Comment

Your email address will not be published.