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?
Asked
Active
Viewed 3,223 times
1

Boris Callens
- 90,659
- 85
- 207
- 305
-
Checkout this post: http://stackoverflow.com/questions/176347/asp-net-mvc-test-controllers-w-sessions-mocking – Darin Dimitrov Feb 25 '09 at 18:42
3 Answers
1
just to channel people out of this sinkhole (which it is as of .NET 3.5) -- don't touch IHttpSessionState for purposes of mocking and testing:

nik.shornikov
- 1,869
- 1
- 17
- 21
1
- 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
- 187,081
- 35
- 232
- 306
-
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. – Boris Callens Jan 23 '09 at 14:09
-
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 Jan 23 '09 at 14:20