ASP.NET MVC 3 ViewResult 子类的单元测试依赖项

发布于 2024-12-23 08:42:53 字数 1533 浏览 2 评论 0原文

我正在创建一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2024-12-30 08:42:53

我弄清楚了如何满足 ViewResult 的最低要求。我遇到的一个问题是嘲笑寻找视图的过程。通过确保填充对象的 View 属性可以避免这种情况。这是我的工作测试:

[TestMethod]
public void TestExecuteAction()
{
    var response = MockRepository.GenerateStub<HttpResponseBase>();
    response.Output = new StringWriter();

    var httpContext = MockRepository.GenerateMock<HttpContextBase>();
    httpContext.Expect(m => m.Response).Return(response);

    var routeData = new RouteData();
    routeData.Values.Add("action", "FakeAction");

    var context = new ControllerContext(httpContext, routeData, MockRepository.GenerateMock<ControllerBase>());

    var viewResult = new JavaScriptViewResult();
    viewResult.View = MockRepository.GenerateMock<IView>();
    viewResult.ExecuteResult(context);

    Assert.AreEqual("text/javascript", context.HttpContext.Response.ContentType);
}

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 the View property of my object was populated. Here is my working test:

[TestMethod]
public void TestExecuteAction()
{
    var response = MockRepository.GenerateStub<HttpResponseBase>();
    response.Output = new StringWriter();

    var httpContext = MockRepository.GenerateMock<HttpContextBase>();
    httpContext.Expect(m => m.Response).Return(response);

    var routeData = new RouteData();
    routeData.Values.Add("action", "FakeAction");

    var context = new ControllerContext(httpContext, routeData, MockRepository.GenerateMock<ControllerBase>());

    var viewResult = new JavaScriptViewResult();
    viewResult.View = MockRepository.GenerateMock<IView>();
    viewResult.ExecuteResult(context);

    Assert.AreEqual("text/javascript", context.HttpContext.Response.ContentType);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文