Right now I’m trying to test Session variables. For example I set:
Session["test"] = "test";
Then:
if (blah) { Session["test"] = "foo"; }
else if (testing) { Session["test"] = "foo2"; }
I’d like to be able to run tests so that depending on the conditions I set forth I can see that the Session variable has changed appropriately. I’ve tried using Moq and I’m able to create a Mock Session and then define a variable to return something specific:
controllerContext.Setup(x => x.HttpContext.Session["test"]).Returns("test");
This doesn’t change though when running through the test.
It seems even further complicated if I’m strongly typing the variables by using a Session wrapper since I try to pass the mock session and context between controllers.
I’ve a read a little about FakeContext and Fake Sessions as opposed to Mock. I’ve also heard a little about using delegates. Really I’m just trying to emulate what happens when the code is actually running and make sure the end result is what I expect and it happens to often depend on my Session variables. What would be the best approach towards testing their modification is properly being done?
It’s good to see that you have used Moq, in which case instead of asserting the value of the session you can assert if the code has attempted to set the session.. Asserting on behaviour basically..
Here are a couple of useful links:
ASP.NET MVC Unit Testing – Sessions
ASP.NET MVC Tip #12 – Faking the Controller Context