joakim0112
joakim0112

Reputation: 33

During testing, controller can't set session variable

I'm using MvcContrib.TestHelper like this:

[TestMethod]
public void LogOut() {

    // Arrange

    var controller = new AdminController(new AdminLogics(new AdminRepositoryStub()));
    var builder = new TestControllerBuilder();
    builder.InitializeController(controller);

    controller.Session["AdminIsLoggedIn"] = true;

    // Act

    var result = (RedirectToRouteResult)controller.LogOut();

    // Assert

    Assert.IsFalse((bool)controller.Session["AdminIsLoggedIn"]);
}

However the assert fails since the session var is not overwritten.

Here's controller:

public ActionResult LogOut() {
    Session["AdminIsLoggedIn"] = false;

    return RedirectToAction("List", "Catalogue");
}

Upvotes: 0

Views: 110

Answers (1)

Danny
Danny

Reputation: 624

Session variables can only be accessed when running under the context of a HttpContext. Your test methods are not running under the HttpContext. It will be difficult to test but perhaps this post will help you.

http://haacked.com/archive/2005/06/11/simulating_httpcontext.aspx

Upvotes: 1

Related Questions