Testing Custom ActionResult

I wrote a special ActionResult that returns a string. I am trying to understand how I can unit test it.

I tried the following:

  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();
  }

This 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 to check the results of actions?

+3
source share
2 answers

Suppose this is not possible for MVC2 or MVC3.

0
source

Perhaps this may help you:

ASP.NET MVC application testing integration without a web server or browser

This is for v1 MVC, I haven't tried it with v2 yet, and I also don't know if there is an update for the integration testing infrastructure, but it might be useful for you.

Thomas

0
source

Source: https://habr.com/ru/post/1780407/


All Articles