title: ASP.NET MVC: Unit testing session variables does't change
body: <p>I'm trying to unit test a Logout action of my controller. My controller recive an interface wich deals with session variables:</p>
  
  <pre><code>public HomeController(IUnitOfWork unitOfWork, ISecurityService security = null) 
  {
      _unitOfWork = unitOfWork;   
  
      if (security != null)
      {
          _securityService = security;
      }
      else
      {
          _userService = new UserService(_unitOfWork)
          _securityService = new SecurityService(_userService);    
      }
  }
  </code></pre>
  
  <p>Using Moq, I create a HttpSessionStateBase object
  Which I use to create a SecurityService object in my unit test class:</p>
  
  <pre><code>var mockSession = new Mock&lt;HttpSessionStateBase&gt;();
  mockSession.SetupGet(s =&gt; s["UserID"]).Returns("1");
  HttpSessionStateBase session = mockSession.Object;
  ISecurityService _securityService = new SecurityService(_userService, session);
  </code></pre>
  
  <p>My SecurityService</p>
  
  <pre><code>public class SecurityService : ISecurityService 
  {
      private readonly IUsuarioService _userService ;
      private readonly HttpSessionStateBase _session;
  
      public int UserID
      {
          get { return Convert.ToInt32(_session["UserID"]); }
          set { _session["UserID"] = value; }
      }
  
      public SecurityService(IUserService service, HttpSessionStateBase session = null)
      {
          _userService = service;
          _session = session ?? new HttpSessionStateWrapper(HttpContext.Current.Session);
      }
  
      public void Logout()
      {
          // I'm trying to delete this variable, but I can't
          _session["UserID"] = 0;
          _session.Clear();
          _session.Abandon();
      }
  }
  </code></pre>
  
  <p>Finally, my test method</p>
  
  <pre><code>[TestMethod]
      public void Should_delete_session_variable_from_the_user()
      {
          controller.Logout();
  
          // It's allways 1
          Assert.IsTrue(Convert.ToInt32(session["UserID"]) == 0);
      }
  </code></pre>
  
  <hr>
  
  <p>When I debug this code, I can see that the session variable does not chage its value. But, if I open a "Quick Watch" window and execute <code>_session["UserID"] = 0;</code>, its value change.  </p>
  
  <p>I can't understand why. This is the first time that I see that a variable does not chage its value in debugging.</p>