30 Jun 2009

Faking and Mocking: HttpContext and HttpSessionState

HttpContext.Current is all well and good until you want to do unit tests on your business logic, and they explode because there's a reference to the Session collection somewhere, which returns null because you're not in a web context. You get to thinking that your business logic should be context-agnostic anyway, and so you want to provide your own context objects for Session, Items, User etc.

So then you run the gamut of HttpContext fakery, thinking about moq, TypeMock etc - only you don't want to go to all that trouble. Phil Haack's HttpSimulator hoves into view but just doesn't feel right for this particular task (although it IS uber cool). His SimulatedHttpRequest class was pretty cool but sadly the Session object was null when I referenced it.

Finally I stumbled on this post (specifically the one by radmanmm). Using the GetMockHttpSessionState() with my SimulatedHttpRequest object solved the problem. Wooot!

PS. Mr Walther's Fake Intrinsic Objects looked pretty cool too, check them out.

No comments:

Post a Comment

Comments are very welcome unless you're a spammer, in which case you should probably kill yourself.

If I helped you out today, you can buy me a beer below. Cheers!