Zooty's Funbag

June 30, 2009

Faking and Mocking: HttpContext and HttpSessionState

Filed under: Uncategorized — zootfroot @ 3:41 pm

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.

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.