views:

29

answers:

1

I've seen a lot of similar threads but none that actually address my particular situation.

I'm writing unit tests in ASP.NET 4.0 web application (ASP.NET Forms, not MVC). There are several spots in the code where I call the ServerVariables collection to call variables like REMOTE_ADDR. Since my unit tests do not actually initiate HttpRequests when executing my code, things like ServerVariables are Null and therefore error when I try to call HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")

All the solutions I've found to address this issue refer to MVC and so they assume that HttpRequest derives from HttpRequestBase, which it does in MVC but not in ASP.NET Forms.

I tried using Moq but you can't mock a sealed class, and HttpRequest is unfortunately sealed with no interface.

+3  A: 

The HttpRequestBase and HttpRequestWrapper classes can be used with a bit of work.

Wherever you currently access HttpContext.Current.Request -- or just plain Page.Request -- you'll need to use an injectable instance of HttpRequestBase instead. Then you'll need to inject a different subclass of HttpRequestBase depending on whether you're testing or live.

  • For live code, you'd probably inject an HttpRequestWrapper instance that wraps HttpContext.Current.Request:

    var liveRequest = new HttpRequestWrapper(HttpContext.Current.Request);
    
  • For test code, you'd need to create and inject your own mock subclass of HttpRequestBase. Presumably Moq can do that for you on-the-fly.

LukeH
Thanks! I am a little hesitant to change the live code though. The live code is currently stable and very complex but was built without a unit testing framework. I'm trying to slowly introduce unit testing into the mix with a new feature I'm adding. I'm worried unit testing will be hard to sell to the rest of my team if I have to change the stable code to conform to the unit tests - even though in test-driven development it would have been written that way from the start.
Adam
+1 for a good answer though. I may still end up going down this road.
Adam
I ended up going a different route and using WatiN for the unit testing. WatiN tests it from the UI layer and so doesn't need to mock the HttpRequest. Thanks for your help. I'm marking yours as the answer anyway since it did in fact answer the question regardless of whether or not that's the direction I went.
Adam