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?
views:
745answers:
2I found using Moq very easy in such situations. +1 to you.
Perpetualcoder
2009-01-23 21:13:02
A:
- Your code should use IHttpSessionState not HttpSessionState.
- 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.
- Replace code aquiring the Session with static delegate that return IHttpSessionState.
- Initialise that static delegate with a function that uses HttpContext.Current.Session.
- During Unit Testing replace delegate with your Mock implementation of IHttpSessionState.
AnthonyWJones
2009-01-23 13:59:02
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
2009-01-23 14:09:19
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
2009-01-23 14:20:44