Is HttpContextWrapper all that....useful?

This blog post explains it pretty well:

http://splinter.com.au/httpcontext-vs-httpcontextbase-vs-httpcontext

The point is that 'vintage' HttpContext does not implement HttpContextBase, and isn't virtual, and therefore cannot be Mocked. HttpContextBase was introduced in 3.5 as a mockable alternative. But there's still the problem that vintage HttpContext doesn't implement HttpContextBase.

So HttpContextWrapper is a handy wrapper class (or 'kludge') that does implement HttpContextBase, and can be used when injecting a 'real' HttpContext using IOC, usually with a factory method like this: () => new HttpContextWrapper(HttpContext.Current)


You should be using the abstract HttpContextBase which is much easier to mock instead of HttpContextWrapper.

public static Func<HttpContextBase> Current = 
    () => new HttpContextWrapper(HttpContext.Current);

And in your unit test:

SomeClass.Current = MockHttpContextBase(); // Sorry I don't know the syntax for Moq