Web Forms – Mocking HttpSession

With thanks to http://stackoverflow.com/users/603670/ben-barreth
at http://stackoverflow.com/questions/1981426/how-do-i-mock-fake-the-session-object-in-asp-net-web-forms

public void SetUpHttpSessionMock()
HttpWorkerRequest _wr = new SimpleWorkerRequest("/dummyWorkerRequest", @"c:\inetpub\wwwroot\dummy", "default.aspx", null, new StringWriter());
HttpContext.Current = new HttpContext(_wr);
HttpSessionStateContainer sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 10, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false);
SessionStateUtility.AddHttpSessionStateToContext(HttpContext.Current, sessionContainer);

One thought on “Web Forms – Mocking HttpSession”

  1. Good day! This post couldn’t be written any better! Reading through this
    post reminds me of my previous room mate! He always kept talking about this.
    I will forward this post to him. Fairly certain he will have a good read.
    Thanks for sharing!

Comments are closed.

Web Forms – Mocking HttpSession

by Chris F Carroll read it in <1 min