Mocking HttpContext for Web API Controllers in ASP.NET 5

In some of our Web API Controllers, we call HttpContext to look at and check the StatusCode on the Response as shown below. Rather than just testing this, I figured it would be better to mock this.

Context.Response.StatusCode = 400;

      

However, all the solutions I could find say to install ControllerContext

on the controller. When I try to do this I don't get ControllerContext

, I just get Context

which is read-only.

Controller Context

Is there a way to mock the HttpContext or do I need to create a fake HttpContext for this?

+3


source to share


1 answer


As of MVC 6, you need to set a property ActionContext

. You can see the property is Context

pulling from there. The constructor ActionContext

will allow you to specify your own mocked HttpContext

.

See also:



+2


source







All Articles