views:

745

answers:

2

I have several asp.net mvc websites consuming the same controller and model.
This common logic is put in seperate libraries. These libraries use the HttpContext.Current.Session. How can I make these unit testable? I read about the StateValue but can't completely grasp it. Where should I get this StateValue thing? Is it a lib I reference?

+1  A: 

You can use mock helpers such as seen here

Otávio Décio
I found using Moq very easy in such situations. +1 to you.
Perpetualcoder
A: 
  1. Your code should use IHttpSessionState not HttpSessionState.
  2. If you look up the MSDN documentation for IHttpSessionState you will find an example implementation you can lift into your Unit Test project to create a mock session.
  3. Replace code aquiring the Session with static delegate that return IHttpSessionState.
  4. Initialise that static delegate with a function that uses HttpContext.Current.Session.
  5. During Unit Testing replace delegate with your Mock implementation of IHttpSessionState.
AnthonyWJones
Although this sounds wonderfull, it will require me to write a complete IHttpSessionState mock. Whilst this StateValue I referenced seems to offer me a system where I don't have to care if there is a session or not anymore.
borisCallens
Since you already have a code base I think you'll find that retro fitting this StateValue stuff is more work than mocking the Session object. Especially as the documentation gives you a good basis for creating the mock to start with.
AnthonyWJones