I am writing a unit test and the controller method is throwing an exception because HttpContext / ControllerContext is null. I don't need to assert anything from the HttpContext, just need it to be not NULL. I have done research and I believe Moq is the answer. But all the samples that I have seen haven't helped me a lot. I don't need to do anything fancy, just to mock the httpcontext. Point me in the right direction!
-
1I think this post will help you out. http://stackoverflow.com/questions/542132/mocking-a-httpcontext-response-output-with-moq – Brian Dishaw Jun 28 '12 at 13:09
-
1Got It! This post helped a lot: http://www.emadibrahim.com/2008/04/04/unit-test-linq-to-sql-in-aspnet-mvc-with-moq/ Made a HttpContextFactory Class with with two methods. One mocking httpcontext and the other setting current controller context to the mocked data. – TheFlyingCircus Jun 29 '12 at 12:47
-
You should put some sample code outlining your solution on here as an answer to your question. Make sure to accept it when you can. – Brian Dishaw Jun 29 '12 at 14:09
2 Answers
Got these two functions from here in a class;
public static class HttpContextFactory
{
public static void SetFakeAuthenticatedControllerContext(this Controller controller)
{
var httpContext = FakeAuthenticatedHttpContext();
ControllerContext context =
new ControllerContext(
new RequestContext(httpContext,
new RouteData()), controller);
controller.ControllerContext = context;
}
private static HttpContextBase FakeAuthenticatedHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
context.Setup(ctx => ctx.User).Returns(user.Object);
user.Setup(ctx => ctx.Identity).Returns(identity.Object);
identity.Setup(id => id.IsAuthenticated).Returns(true);
identity.Setup(id => id.Name).Returns("a.ali174");
return context.Object;
}
}
From the unit test I called them as such;
HttpContextFactory.SetFakeAuthenticatedControllerContext(controller);
Make sure you have Moq installed in your tests project:
Install-Package Moq

- 5,911
- 4
- 29
- 49

- 121
- 1
- 5
I know this is an older subject, however Mocking a MVC application for unit tests is something we do on very regular basis.
I just wanted to add my experiences Mocking a MVC 3 application using Moq 4 after upgrading to Visual Studio 2013. None of the unit tests were working in debug mode and the HttpContext was showing "could not evaluate expression" when trying to peek at the variables.
Turns out visual studio 2013 has issues evaluating some objects. To get debugging mocked web applications working again, I had to check the "Use Managed Compatibility Mode" in Tools=>Options=>Debugging=>General settings.

- 3,066
- 2
- 25
- 36