开发者

Test custom ActionResult

开发者 https://www.devze.com 2023-01-30 22:04 出处:网络
I\'ve written a custom ActionResult which returns a string. I\'m trying to figure out how I can unit test it.

I've written a custom ActionResult which returns a string. I'm trying to figure out how I can unit test it.

I've tried with the following:

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


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

It gives me:

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

Stack trace:

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

How do I test action results?


Guess it's not possible for MVC2 or MVC3.


Maybe this can help you:

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

It's for v1 of MVC, I haven't tried it yet with v2 and I also don't know if there's an update for the integration testing framework, but maybe it can be useful for you.

Thomas

0

精彩评论

暂无评论...
验证码 换一张
取 消