title: Unable to set session variable
body: <p>I have been unsuccessful in attempting to set a session variable in order to run some unit tests. I keep getting the error of "System.NullReferenceException: Object reference not set to an instance of an object" when I attempt to set my session variable. </p>
  
  <p>Here is the test I'm building:</p>
  
  <pre><code>[TestMethod]
  public void MyMethod()
  {
    //Arrange
    int id = 12345;
    string action = "A";
    string comment = "";
    string user = "user";
    var controller = new MyController();
  
    //Act
    controller.Session["altUser"] = user;
    var result = controller.Process(id, action, comment);
  
    //Assert
    Assert.IsNotNull(result);     
  }
  </code></pre>
  
  <p>And here is my controller:</p>
  
  <pre><code>[Authorize]
  public class MyController : Controller
  {
    public ActionResult Process(int id, string action, string comment)
    {
      string userId = Session["altUser"].ToString();
      //some other stuff that evaluates ID, Action, and Comment
    }
  }
  </code></pre>
  
  <p>However, when I run the application itself, there are no errors and the application functions as it should. I do understand that with Test Driven Development the tests should pave the way for the implementation. I am trying to get some practice with Unit Testing on applications that have already been done. If at all possible, since the application works, I would like to avoid making any changes to my implementation and just write a unit test to support what I already know.</p>