Skip to content
Fake implementation of HttpContextBase for terse and expressive tests without the tedious setup of a mocking framework
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Fake implementation of HttpContextBase

For terse and expressive tests without the tedious setup of a mocking framework. Tedious because the HttpContextBase object graph is deep and rightfully riddled with NotImplementedException.

Can't I just roll my own?

Yes you can, and you'll have plenty of company.

  1. MvcContrib
  2. Oxite
  3. MVCFakes
  4. ASP.NET MVC Session at Mix08, TDD and MvcMockHelpers

Each of these projects had the same idea yet they all have subtly different interfaces. NuGet is about unifying community fragmentation and making it easy to share and reuse code. This project intentionally has no dependencies on Asp.Net MVC or any mocking frameworks.



public void Url_query_string_should_add_to_query_string_collection()
    var url = new Uri("");

    var request = new FakeHttpRequest(url);

    Assert.That(request.QueryString["q"], Is.EqualTo("awesome"));
    Assert.That(request.QueryString["p"], Is.EqualTo("1"));

public void Can_access_query_string_values_by_default_indexer()
    var request = new FakeHttpRequest();

    request.QueryString.Add("id", "3");

    Assert.That(request["id"], Is.EqualTo("3"));


public void Can_access_form_values_by_default_indexer()
    var request = new FakeHttpRequest();

    request.Form.Add("color", "blue");

    Assert.That(request["color"], Is.EqualTo("blue"));


public void Should_add_values_to_session()
    var session = new FakeHttpSessionState { { "color", "red" } };

    Assert.That(session["color"], Is.EqualTo("red"));


public void User_should_not_be_authenticated()
    var principal = new FakeHttpContext().User;

    Assert.That(principal.Identity.IsAuthenticated, Is.False);

public void User_can_be_authenticated_with_convenience_method()
    var context = new FakeHttpContext().Authenticate();

    Assert.That(context.User.Identity.IsAuthenticated, Is.True);
    Assert.That(context.Request.IsAuthenticated, Is.True);

You can also set any virtual property from one of the Http* classes, even those that aren't explicitly settable. e.g., for HttpContext:

var context = new FakeHttpContext();
var uri = new Uri("");

//set the UrlReferrer
context.Set(ctx => ctx.UrlReferrer, uri);

//get the UrlReferrer
Console.WriteLine(context.UrlReferrer); //<>

This means no more NotImplementedExceptions in your tests.

Something went wrong with that request. Please try again.