12

I need a mock of HttpContext for unit testing. But I'm struggling with it.

I'm making a method that would change sessionId by programmatically with SessionIdManager. And SessionIdManager requires HttpContext not HttpContextBase.

But I couldn't find any example to make a mock of HttpContext. All examples out there are only to make HttpContextBase.

I tried below but they didn't work

HttpContext httpContext = Mock<HttpContext>();
HttpContext httpContext = (HttpContext)GetMockHttpContextBase();

public HttpContextBase GetMockHttpContextBase()
{
   var context = new Mock<HttpContextBase>();
   var request = new Mock<HttpRequestBase>();
   var response = new Mock<HttpResponseBase>();
   var session = new Mock<HttpSessionStateBase>();
   var application = new Mock<HttpApplication>();
   var httpContext = new Mock<HttpContext>();
   var server = new Mock<HttpServerUtilityBase>();
   var user = new Mock<IPrincipal>();
   var identity = new Mock<IIdentity>();
   var urlHelper = new Mock<UrlHelper>();
   var routes = new RouteCollection();
   var requestContext = new Mock<RequestContext>();

   requestContext.Setup(x => x.HttpContext).Returns(context.Object);
   context.Setup(ctx => ctx.Request).Returns(request.Object);
   context.Setup(ctx => ctx.Response).Returns(response.Object);
   context.Setup(ctx => ctx.Session).Returns(session.Object);
   application.Setup(x => x.Context).Returns(httpContext.Object);
   context.Setup(ctx => ctx.ApplicationInstance).Returns(application.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("test");
   request.Setup(req => req.Url).Returns(new Uri("http://tempuri.org"));
   request.Setup(req => req.RequestContext).Returns(requestContext.Object);
   requestContext.Setup(x => x.RouteData).Returns(new RouteData());
   request.SetupGet(req => req.Headers).Returns(new NameValueCollection());

   return context.Object;
}

Is there any way to make mock of HttpContext or to use HttpContextBase for HttpContext?

Please help me anyone.

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
genki98
  • 680
  • 1
  • 11
  • 31
  • See also: http://stackoverflow.com/a/2497618/126014 – Mark Seemann Mar 24 '14 at 08:43
  • But like I said, your links are all about HttpContextBase, not HttpContext. I need a httpContext for 'new SessionIDManager().GetSessionID(HttpContext)'. Thanks anyway. – genki98 Mar 24 '14 at 08:53
  • Possibly related: http://stackoverflow.com/q/1992141/126014 – Mark Seemann Mar 24 '14 at 09:12
  • possible duplicate: http://stackoverflow.com/q/18182598/126014 – Mark Seemann Mar 24 '14 at 09:13
  • I tried your 3rd links before, as you could see in my sample code. But it didn't work cause new Mock() throws runtime exception and even without it HttpContextBase.ApplicationInstance.Context returns null. So it didn't work. And I'm using vs 2010 so 4th link couldn't help me also. Appreciate your help though. – genki98 Mar 24 '14 at 09:24
  • What do you mean by "they didn't work"? You got errors? It couldn't find your mocked object? Your tests produced unexpected results? – Chris Mar 24 '14 at 14:06
  • Yes, GetMockHttpContextBase() is my method to get mock HttpContextBase and if I run it, then there is a runtime exception on 'var httpContext = new Mock();' and if I delete it, then I get only null from HttpContextBase.ApplicationInstance.Context. – genki98 Mar 25 '14 at 00:26

2 Answers2

15

This is a common question here and the root of the problem is DON'T MOCK TYPES YOU DON'T OWN. Rather than complicated mocks to try and reproduce the behavior of a class you didn't write (which means you're guessing about what it does and what you need to mock), introduce an abstraction between your code and the external object HttpContext and mock that abstraction. As one of the pioneers of mock objects says:

The key for me was when Joe Walnes came up with the radical notion of "Don't mock types you don't own". This means: stop jumping through hoops to work with closed-world libraries, use the tests to discover your objects and what they say to each other.

http://higherorderlogic.com/2004/02/the-big-idea-is-messaging/

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33
5

If you're set on trying to mock HttpContext, then you're going to have to use quite a bit of boilerplate to essentially simulate it.

Phil Haack describes this approach in good detail in this blog post: http://haacked.com/archive/2005/06/11/simulating_httpcontext.aspx/

As I stated before, I wanted a “lightweight” means to test this method. There wouldn’t be a problem if HttpContext.Current was a valid instance of HttpContext. Luckily, the static Current property of HttpContext is both readable and writeable. All it takes is to set that property to a properly created instance of HttpContext. However, creating that instance wasn’t as straightforward as my first attempt. I’ll spare you the boring details and just show you what I ended up with.

He provides a simulator framework here: http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx/

Keep in mind that you're likely venturing into integration testing with all of this, as the logic involved in simulating these objects is non-trivial and can interfere with the reliability of your tests.

Lilshieste
  • 2,744
  • 14
  • 20
  • Even though I chose Mike's reply as an answer, this is closer to what I expected. Very good detailed answer, thank you Lilshieste. – genki98 Mar 25 '14 at 00:22