ASP.NET MVC 3 ViewResult 子类的单元测试依赖项
我正在创建一个名为 JavaScriptViewResult
的非常简单的 ViewResult
子类,它在执行时调用基本实现,然后设置对 text/javascript
的响应。在尝试对此类进行单元测试时,我在满足 ASP.NET MVC 堆栈的所有依赖项时遇到了一系列困难。
这是我使用 Rhino 的单元测试到目前为止的样子:
[TestMethod]
public void TestExecuteAction()
{
var request = MockRepository.GenerateMock<HttpRequestBase>();
request.Expect(m => m.Url).Return(new Uri("/Test/JavaScript", UriKind.Relative));
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
httpContext.Expect(m => m.Request).Return(request);
var controller = MockRepository.GenerateMock<ControllerBase>();
var virtualPathProvider = MockRepository.GenerateMock<VirtualPathProvider>();
var routeCollection = new RouteCollection(virtualPathProvider);
routeCollection.MapRoute("FakeRoute", "Test/JavaScript", new { controller = "Test", action = "JavaScript" });
var routeData = routeCollection.GetRouteData(httpContext);
var context = new ControllerContext(httpContext, routeData, controller);
var viewResult = new JavaScriptViewResult();
viewResult.ExecuteResult(context);
Assert.AreEqual("text/javascript", context.HttpContext.Response.ContentType);
}
运行测试时的最新异常是 System.Web.Routing.Route.GetRouteData 内部深处的
。NullReferenceException
(HttpContextBase httpContext)
如何设置执行 ViewResult
的所有依赖项?有什么技术可以让这个变得更简单吗?或者,是否有其他方法可以利用 MVC 视图引擎生成 JavaScript,为响应设置正确的 Content-Type
?
I'm creating a really simple ViewResult
subclass called JavaScriptViewResult
that, when executing, calls the base implementation and then sets the Content-Type
of the response to text/javascript
. In trying to unit test this class, I'm running across a slew of difficulties fulfilling all of the dependencies of the ASP.NET MVC stack.
Here is what my unit test, which uses Rhino, looks like so far:
[TestMethod]
public void TestExecuteAction()
{
var request = MockRepository.GenerateMock<HttpRequestBase>();
request.Expect(m => m.Url).Return(new Uri("/Test/JavaScript", UriKind.Relative));
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
httpContext.Expect(m => m.Request).Return(request);
var controller = MockRepository.GenerateMock<ControllerBase>();
var virtualPathProvider = MockRepository.GenerateMock<VirtualPathProvider>();
var routeCollection = new RouteCollection(virtualPathProvider);
routeCollection.MapRoute("FakeRoute", "Test/JavaScript", new { controller = "Test", action = "JavaScript" });
var routeData = routeCollection.GetRouteData(httpContext);
var context = new ControllerContext(httpContext, routeData, controller);
var viewResult = new JavaScriptViewResult();
viewResult.ExecuteResult(context);
Assert.AreEqual("text/javascript", context.HttpContext.Response.ContentType);
}
The latest exception when running the test is a NullReferenceException
deep within the bowels of System.Web.Routing.Route.GetRouteData(HttpContextBase httpContext)
.
How do I set up all of the dependencies for executing a ViewResult
? Are there any techniques for making this simpler? Alternately, is there a different way I can utilize the MVC view engine to generate JavaScript that will set the proper Content-Type
for the response?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我弄清楚了如何满足
ViewResult
的最低要求。我遇到的一个问题是嘲笑寻找视图的过程。通过确保填充对象的View
属性可以避免这种情况。这是我的工作测试:I figured out how to meet the minimum requirements of
ViewResult
. One problem I was encountering was mocking the process of finding the view. This was avoidable by ensuring that theView
property of my object was populated. Here is my working test: