First, I recommend that you isolate your code as much as possible from HttpContext.Current ; this will not only make your code more suitable for testing, but will help prepare you for ASP.NET vNext, which is more like OWIN (without HttpContext.Current ).
However, this may require many changes that you are not ready for. To make fun of HttpContext.Current , you need to understand how this works.
HttpContext.Current is a per-thread variable that is managed by ASP.NET SynchronizationContext . This SynchronizationContext is a "request context" representing the current request; it is created by ASP.NET when a new request arrives. I have an MSDN article on SynchronizationContext if you are interested in more details.
As I explain in my async intro blog post , when you await a Task , by default it will capture the current "context" and use this to resume the async method. When the async method runs in the context of an ASP.NET request, the "context" captured by await is an ASP.NET SynchronizationContext . When the async method resumes (possibly in a different thread), ASP.NET SynchronizationContext set HttpContext.Current before resuming the async method. This is how async / await works on an ASP.NET host.
Now, when you run the same code in unit test, the behavior is different. In particular, there is no ASP.NET SynchronizationContext for setting HttpContext.Current . I assume your unit test method returns Task , in which case NUnit does not provide a SynchronizationContext at all. So, when the async method is resumed (possibly in a different thread), its HttpContext.Current may not be the same.
There are several different ways to fix this. One option is to write your own SynchronizationContext , which saves HttpContext.Current , like ASP.NET. A simpler (but less efficient) option is to use a SynchronizationContext , which I wrote under the name AsyncContext , which ensures that the async method resumes in the same thread, you should be able to install my AsyncEx library from NuGet , and then wrap your unit methods test during an AsyncContext.Run call. Note that unit test methods are now synchronous:
[Test] public void MyTest() { AsyncContext.Run(async () => {