[英]Unit testing HtmlHelper extension method fails
I am trying to test some HtmlHelper
extension methods I have written. 我正在尝试测试我编写的一些HtmlHelper
扩展方法。 My first problem was how to create a HtmlHelper
instance, but I solved that using this code: 我的第一个问题是如何创建HtmlHelper
实例,但我使用此代码解决了这个问题:
private static HtmlHelper<T> CreateHtmlHelper<T>(T model)
{
var viewDataDictionary = new ViewDataDictionary(model);
var controllerContext = new ControllerContext(new Mock<HttpContextBase>().Object,
new RouteData(),
new Mock<ControllerBase>().Object);
var viewContext = new ViewContext(controllerContext, new Mock<IView>().Object, viewDataDictionary, new TempDataDictionary(), new Mock<TextWriter>().Object);
var mockViewDataContainer = new Mock<IViewDataContainer>();
mockViewDataContainer.Setup(v => v.ViewData).Returns(viewDataDictionary);
return new HtmlHelper<T>(viewContext, mockViewDataContainer.Object);
}
Several of my tests now work fine, but there is one test that throws an exception. 我的几个测试现在工作正常,但有一个测试会引发异常。 The test is defined as follows: 测试定义如下:
// Arrange
var inputDictionary = CreateDictionary();
var htmlHelper = CreateHtmlHelper(inputDictionary);
// Act
var actualHtmlString = htmlHelper.EditorFor(m => m.Dict, model).ToHtmlString();
...
The EditorFor
method is my extension method. EditorFor
方法是我的扩展方法。 Somewhere in that method, the following call is made: 在该方法的某处,进行以下调用:
tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(expression, metadata));
It is that when this code is executed from my unit test that the following exception is thrown: 从我的单元测试执行此代码时,抛出以下异常:
System.NullReferenceExceptionObject reference not set to an instance of an object.
at System.Web.Mvc.ViewContext.ScopeCache.Get(IDictionary`2 scope, HttpContextBase httpContext)
at System.Web.Mvc.ViewContext.get_UnobtrusiveJavaScriptEnabled()
at System.Web.Mvc.HtmlHelper.GetUnobtrusiveValidationAttributes(String name, ModelMetadata metadata)
at AspNetMvcDictionarySerialization.HtmlHelperExtensions.InputTagHelper(HtmlHelper htmlHelper, ModelMetadata metadata, InputType inputType, String expression, IDictionary`2 htmlAttributes, String fullName, Int32 index, String fieldType, String val) in HtmlHelperExtensions.cs: line 154
So the code fails in ScopeCache.Get
, but why? 所以代码在ScopeCache.Get
失败了,但为什么呢? Does anyone have any idea how to solve this? 有谁知道如何解决这个问题?
What I ended up doing was looking at the source code of ASP.NET MVC . 我最终做的是查看ASP.NET MVC的源代码 。 In their code, they also test HtmlHelper
instances. 在他们的代码中,他们还测试HtmlHelper
实例。 They do so using a utility class named MvcHelper
, which provides convenience methods to create new HtmlHelper
instance with a correctly prepared HTTP context. 他们使用名为MvcHelper
的实用程序类来MvcHelper
,它提供了使用正确准备的HTTP上下文创建新HtmlHelper
实例的便捷方法。
After removing the code I did not need, I ended up with the following class: 删除我不需要的代码后,我最终得到了以下类:
public static class MvcHelper
{
public static HtmlHelper<TModel> GetHtmlHelper<TModel>(TModel inputDictionary)
{
var viewData = new ViewDataDictionary<TModel>(inputDictionary);
var mockViewContext = new Mock<ViewContext> { CallBase = true };
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
mockViewContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
return new HtmlHelper<TModel>(mockViewContext.Object, GetViewDataContainer(viewData));
}
public static IViewDataContainer GetViewDataContainer(ViewDataDictionary viewData)
{
var mockContainer = new Mock<IViewDataContainer>();
mockContainer.Setup(c => c.ViewData).Returns(viewData);
return mockContainer.Object;
}
}
With this helper class, my code executed correctly. 使用此帮助程序类,我的代码正确执行。
I have created a gist for the complete helper class to allow for easy inclusion in your project: https://gist.github.com/ErikSchierboom/6da474dcd5751fbbc94c 我已经为完整的帮助程序类创建了一个要点,以便轻松包含在您的项目中: https : //gist.github.com/ErikSchierboom/6da474dcd5751fbbc94c
看起来你可能也需要模拟HttpContext。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.