I am trying to make fun of a controller session variable using Moq. What I keep working on is that the session is visible from the test, but zero inside the actual controller.
Test code:
[TestMethod]
public void SessionTest()
{
var controller = new BaseController();
var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(cc => cc.HttpContext.Session["user"]).Returns(new User());
controller.ControllerContext = controllerContext.Object;
User currentUser = controller.CurrentUser;
User currentUser = (User)controller.Session["test"];
}
Controller Code:
public User CurrentUser
{
get
{
return (User)Session["user"]; //HttpContext is null at this point
}
}
The code above follows the most common pattern that I saw (several SO, MVC 2 messages in Example 7, etc.), but it still doesn't work. Inside the controller, ControllerContext is NULL, just like HttpContext. What am I doing wrong?
source
share