测试自定义ActionResult

时间:2010-12-15 11:04:33

标签: asp.net-mvc unit-testing

我编写了一个返回字符串的自定义ActionResult。我正在试图弄清楚如何对它进行单元测试。

我尝试了以下内容:

  string ExecuteResult(ActionResult result)
  {
        var ctx = new ControllerContext();
        var writer = new StringWriter();
        var response = new HttpResponse(writer);
        var context = new HttpContext(new HttpRequest(null, "http://localhost/uri/", null), response);
        System.Web.HttpContext.Current = context;


        result.ExecuteResult(ctx);
        return writer.ToString();
  }

它给了我:

Test method Tests.Web.Mvc.ApplicationControllerTest.TestMessageBox threw exception: 
System.NotImplementedException: The method or operation is not implemented.

堆栈追踪:

System.Web.HttpContextBase.get_Response()
System.Web.Mvc.JavaScriptResult.ExecuteResult(ControllerContext context)
Tests.Web.Mvc.ResultExecutor.InvokeActionResult(ActionResult result) in D:\utv\Framework 4.0\Main\src\Framework.Tests\Web\Mvc\ResultExecutor.cs: line 22
Tests.Web.Mvc.ApplicationControllerTest.TestMessageBox() in D:\utv\Framework 4.0\Main\src\Framework.Tests\Web\Mvc\ApplicationControllerTest.cs: line 46

如何测试操作结果?

2 个答案:

答案 0 :(得分:0)

也许这可以帮到你:

Integration testing an ASP.NET MVC application without web server or browser

对于MVC的v1,我还没有用v2尝试过,我也不知道是否有集成测试框架的更新,但它可能对你有用。

托马斯

答案 1 :(得分:0)

猜测MVC2或MVC3是不可能的。