Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Created November 28, 2012 21:05
Show Gist options
  • Save bradoyler/4164480 to your computer and use it in GitHub Desktop.
Save bradoyler/4164480 to your computer and use it in GitHub Desktop.
A way to get a HttpContext that is able to be mocked out via HttpContextBase
public class HttpContextFactory
{
private static HttpContextBase context;
public HttpContextFactory() {}
public void setHttpContext(HttpContextBase c)
{
context = c;
}
public HttpContextBase getContext()
{
if (context == null)
{
return new HttpContextWrapper(HttpContext.Current);
}
return context;
}
}
@bradoyler
Copy link
Author

In your app you use like: var context = new HttpContextFactory().getContext();
In your tests (using Moq) you use like:
var request = new Mock();
mockContext.SetupGet(c => c.Request).Returns(request.Object);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment