title: ASP.NET MVC Unit Testing - Sessions
body: <p>Having searched StackOverflow, and Google I think what I'm doing is suppose to be right, however results don't seem to be going well</p>
  
  <pre><code>    [TestMethod]
      public void LoginAction_Should_Return_View_and_User_Authenticated()
      {
          // Arrange
          var mock = new Mock&lt;ControllerContext&gt;();
          var mockSession = new Mock&lt;HttpSessionStateBase&gt;();
          mock.Setup(p =&gt; p.HttpContext.Session).Returns(mockSession.Object);
  
          var testData = FakeUserData.CreateTestUsers();
          var repository = new FakeUserRepository(testData);
          var controller = new AccountController(repository);
          controller.ControllerContext = mock.Object;
  
          // Act
          var result = controller.Login("testuser1", "testuser1");
  
          // Assert
          Assert.AreEqual("testuser1", controller.HttpContext.Session["Username"]);
          Assert.IsTrue((bool)controller.HttpContext.Session["IsAuthenticated"]);
          Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
      }
  </code></pre>
  
  <p>When I run the test the value of controller.HttpContext.Session["Username"] is null, however I set the value to the username using a Session helper.  Am I doing something completely wrong, or something else? Any help would be greatly appreciated.</p>