Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fake implementation of HttpContextBase for terse and expressive tests without the tedious setup of a mocking framework
C#
Branch: master

Merge pull request #5 from tmont/master

Use some fancy expression tree magic to make all virtual properties settable
latest commit 8aec42a116
@mikevalenty authored
Failed to load latest commit information.
packages Fixed hing path for NUnit
src/Web added Set<T> extension method for each FakeHttp* class that enables y…
test/Web added Set<T> extension method for each FakeHttp* class that enables y…
.gitignore Initialized repository
FakeN.sln Initialized repository
LICENSE.txt
README.markdown added example on how to use the Set<T> extension method

README.markdown

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.

Examples:

QueryString:

[Test]
public void Url_query_string_should_add_to_query_string_collection()
{
    var url = new Uri("http://google.com?q=awesome&p=1");

    var request = new FakeHttpRequest(url);

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

[Test]
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"));
}

Form:

[Test]
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"));
}

Session:

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

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

User:

[Test]
public void User_should_not_be_authenticated()
{
    var principal = new FakeHttpContext().User;

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

[Test]
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("http://www.google.com/");

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

//get the UrlReferrer
Console.WriteLine(context.UrlReferrer); //<http://www.google.com/>

This means no more NotImplementedExceptions in your tests.

Something went wrong with that request. Please try again.