How do I mock the HttpContext in ASP.NET MVC using Moq?


Create a request, response and put them both to HttpContext:

HttpRequest httpRequest = new HttpRequest("", "http://mySomething/", "");
StringWriter stringWriter = new StringWriter();
HttpResponse httpResponse = new HttpResponse(stringWriter);
HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse);


public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist()
    var context = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
        .Setup(c => c.Request)
    HomeController controller = new HomeController();

    controller.HttpContext = context; //Here I am getting an error (read only).

my base controller has an overrride of the Initialize that get's this requestContext. I am trying to pass this along but I am not doing something right.

protected override void Initialize(System.Web.Routing.RequestContext requestContext)

Where can I get more information on mocking my RequestContext and HttpContext using Moq? I am trying to mock cookies and the general context.

Mock HttpContext using moq for unit test

This is a common question here and the root of the problem is DON'T MOCK TYPES YOU DON'T OWN. Rather than complicated mocks to try and reproduce the behavior of a class you didn't write (which means you're guessing about what it does and what you need to mock), introduce an abstraction between your code and the external object HttpContext and mock that abstraction. As one of the pioneers of mock objects says:

The key for me was when Joe Walnes came up with the radical notion of "Don't mock types you don't own". This means: stop jumping through hoops to work with closed-world libraries, use the tests to discover your objects and what they say to each other.

Fake Http Context for testing

It is very hard to reasonably mock non *Base versions of the HttpContext and related classes. Consider switching your code to use *Base classes so you can actually mock them. You can easyly convert HttpContext to *Base version with HttpContextWrapper (, but there is no built in way of doing reverse.

In your particular case consider passing in informaion that is really needed for the call (i.e. context.request.UserHostAddress) instead of whole HttpContext object.

Testing a Web API method that uses HttpContext.Current.Request.Files?

Web API has been built to support unit testing by allowing you to mock various context objects. However, by using HttpContext.Current you are using "old-style" System.Web code that uses the HttpContext class which makes it impossible to unit test your code.

To allow your code to be unit testable you have to stop using HttpContext.Current. In Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME you can see how to upload files using Web API. Ironically, this code also uses HttpContext.Current to get access to the MapPath but in Web API you should use HostingEnvironment.MapPath that also works outside IIS. Mocking the later is also problematic but for now I am focusing on your question about mocking the request.

Not using HttpContext.Current allows you to unit test your controller by assigning the ControllerContext property of the controller:

var content = new ByteArrayContent( /* bytes in the file */ );
content.Headers.Add("Content-Disposition", "form-data");
var controllerContext = new HttpControllerContext {
  Request = new HttpRequestMessage {
    Content = new MultipartContent { content }
var controller = new MyController();
controller.ControllerContext = controllerContext;