I've seen how to Fake the SessionState object in MVC using Scott Hanselmans MvcMockHelpers, but I'm dealing with a separate problem.
What I like to do is create a wrapper around the Session object to make objects a little more accessible and strongly typed rather than using keys all over. Here is basically what it does:
public class SessionVars
{
public SessionVars()
{}
public string CheckoutEmail
{
get { return Session[checkoutEmailKey] as string; }
set { Session[checkoutEmailKey] = value; }
}
}
So that I can just do this in my controllers and views:
SessionVars s = new SessionVars();
s.CheckoutEmail = "[email protected]";
Now the problem comes in when I want to write unit tests, this class is tightly coupled with the HttpSessionState. What I can't figure out is what is the right class to accept/pass so that I can pass in a FakeHttpSession into the SessionVars class. I've tried so many things with this, and this (below) will compile, but it can't cast the HttpSessionState into the IDictionary. I've tried ICollection, HttpSessionStateBase.
public class SessionVars
{
public SessionVars() : this(HttpContext.Current.Session) { }
public SessionVars(ICollection session)
{
Session = (IDictionary<string, object>)session;
}
public IDictionary<string, object> Session
{
get;
private set;
}
public string CheckoutEmail
{
get { return Session[checkoutEmailKey] as string; }
set { Session[checkoutEmailKey] = value; }
}
public Order Order
{
get { return Session[orderKey] as Order; }
set { Session[orderKey] = value; }
}
}
I'm missing something big here. I feel like this is possible and that I should even be that far off.