I have recently Implemented a unit of work pattern, and as an environment we are using more unit testing. Currently the implementation writes into a session helper that writes to session. How do I unit test these aspects in regard to the session? Should I make a repository pattern? (repository interface with concrete session implementation and concrete mock implementation) How is this normally done?
I know there is probably more than one way of approaching this, but I am just looking for some advice.
There are basically two ways of doing this.
Assuming you are using .NET 3.5 or up. Change your implementation to take the HttpSessionStateBase object as a constructor parameter, you can then mock this implementation – there’s a few tutorials online on how to do this. You can then use an IoC container to wire this up at app start or do something like (poor man’s dependency injection):
Alternatively, and probably a bit better and more flexible design would be to create a test seam by wrapping your interaction with session in another object. You could then change this to a database, cookie or cache based implementation later. Something like:
You can then use Moq to create a mock IStateStorage implementation for your tests or create a simple dictionary based implementation.
Hope that helps.