Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set session variable

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.

Here is the test I'm building:

[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);     
}

And here is my controller:

[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
  }
}

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.

like image 888
Adam Bartz Avatar asked May 24 '26 17:05

Adam Bartz


1 Answers

The controller gets the session from the HttpContext, which does not exist in your unit test, which is why it fails.

You can, however, mock an HttpContext and put a mock session in there too.

Something like this might work (uses moq as the Mocking framework)

    var mockControllerContext = new Mock<ControllerContext>();
    var mockSession = new Mock<HttpSessionStateBase>();
    mockSession.SetupGet(s => s["altUser"]).Returns("user");
    mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);

    var controller = new MyController();
    controller.ControllerContext = mockControllerContext.Object;

You'll obviously need to fill the mock object with the details of what you actually want to get out.

You can also derive your own classes from HttpSessionStateBase and HttpContextBase and use them instead of the real session.

like image 198
Colin Mackay Avatar answered May 26 '26 06:05

Colin Mackay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!