Created
November 28, 2012 21:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);