I'm trying to figure out how to unit test my object that persists itself to session state.
The controller does something like...
[HttpPost]
public ActionResult Update(Account a)
{
SessionManager sm = SessionManager.FromSessionState(HttpContext.Current.Session);
if ( a.equals(sm.Account) )
sm.Account = a;
return View("View", a);
}
And the session manager serializes itself to session state
public class SessionManager
{
public static SessionManager FromSessionState(HttpSessionStateBase state)
{
return state["sessionmanager"] as SessionManager ?? new SessionManager();
}
public Guid Id { get; set; }
public Account Account { get; set; }
public BusinessAssociate BusinessAssociate {get; set; }
}
I'm thinking that there are two approaches to unit testing...
in the static instantiator pass in the session state reference and then restore from that. this would allow me to mock this parameter and everything would be golden.
public static SessionManager FromSessionState(HttpSessionStateWrapper session) { return session["sessionmanager"] as SessionManager ?? new SessionManager(); }
create a mock for the SessionManager and use that for testing of the controller.