Fake/Mock HttpContext without any special mocking framework

When writing unit tests, we often need to find a way to mock concrete classes of the .NET Framework in order to make our tests truly unit tests rather than integration tests, and although there are frameworks which allows one to mock some of these classes, sometimes you just can’t (or don’t want to) take a dependency on one of these mocking frameworks.

The HttpContext (System.Web.HttpContext) class is one of those classes that I find myself mocking a lot when writing unit tests for ASP .NET MVC applications and services that rely on authentication, caching, cookies, characteristics of a request, and other ASP .NET features.

The way I’ve been mocking HttpContext (and works very well) when I can’t use a mocking framework, is to just create an HttpContext instance with an HttpRequest instance pointing to a URL, and an HttpResponse instance with no TextWriter to write to. Something like this:

HttpContext.Current = new HttpContext(
    new HttpRequest(null, "http://tempuri.org", null),
    new HttpResponse(null));

The URL of the request can by any valid URL, but I tend to use “http://tempuri.org” as it is the test default namespace URI used by Microsoft development products.

One important thing to keep in mind when doing this is that, because HttpContext.Current is a static resource shared among multiple tests that you can run in a test suite, it is important to reset the HttpContext.Current it to the original value (null) before the next test runs, or you might get unexpected test results due to changes to the HttpContext.Current made by previous tests.

Assuming the tests won’t run in parallel, it is easy to just create the fake HttpContext instance before each test runs and reset it after each test finishes. Using NUnit, it would be something like this:

[SetUp]
public void SetUp()
{
    HttpContext.Current = new HttpContext(
        new HttpRequest(null, "http://tempuri.org", null),
        new HttpResponse(null));
}
[TearDown]
public void TearDown()
{
    HttpContext.Current = null;
}

This allows me to write/read cookies, add/remove items to the cache, and more, which means I can indirectly mock these ASP .NET features and test my code very easily, without any special mocking framework, runtime, or webserver involved.

Of course, things would be much easier if Microsoft would just provide us with a IHttpContext interface that we could mock directly, but until then, this trick is pretty handy!

This entry was posted in .NET, Unit Testing and tagged , , , , , , . Bookmark the permalink.

8 Responses to Fake/Mock HttpContext without any special mocking framework

  1. Alex Howle says:

    Awesome! Thanks for this!

  2. Samuel says:

    System.Web.HttpContextBase

  3. Paulo Gonçalves says:

    How to solve the Server.MapPath() problem when using this solution? Should point to localhost in url?

  4. George says:

    Awesome!

  5. Adarsh Kumar says:

    Thank a lot dude. It helped me :)

  6. Alex says:

    What about the session object, is it possible, seems like session is readonly

  7. AKS says:

    Wow…worked like a charm..nice article. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>