Testing ASP.NET session cache

I was asked how to test the ASP.NET Page.Session object. I’m sure there are many ways of doing it, but here is my quick console sample using TypeMock;

 static void Main(string[] args)
        {
            Page page = new Page();
            MockManager.Init();
            MockObject sessionMock = MockManager.MockObject(typeof(HttpSessionState));
            Mock pageMock = MockManager.MockAll(page.GetType());
            pageMock.ExpectGet("Session", sessionMock.Object);
            sessionMock.ExpectGetIndex("bert");
            string value = (string)page.Session["bert"];
            Console.ReadKey();
            MockManager.Verify();
            MockManager.ClearAll();
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s