1

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?

Boris Callens
  • 90,659
  • 85
  • 207
  • 305

3 Answers3

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:

http://www.codemerlin.com/2011/07/mocking-httpcontext-httpresponse-httprequest-httpsessionstate-etc-in-asp-net/

nik.shornikov
  • 1,869
  • 1
  • 17
  • 21
1

You can use mock helpers such as seen here

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
1
  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
  • 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