2

i'm using an example in which i can fake the session. It's for use in a unittest of a mvc controller.

In the test i create a controller and then i do this:

FakeHttpContext  httpctx  = new FakeHttpContext(null,null,null,null,mSessionItems );
ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);

where mSessionItems is my session and target is my instance of a controller

and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session, i have a session, great!

but.... i also read the session outside the controller, and there i use HttpContext.Current.Session, and that is null (or actualy, the HttpContext.Current is null).

So i wondered, what is the difference between the two?

Michel
  • 23,085
  • 46
  • 152
  • 242

1 Answers1

2

ControllerContext.HttpContext is of the abstract type HttpContextBase. Default implementation of this type (HttpContextWrapper) wraps access to HttpContext. So when you create the fake implementation you are replacing its relation to HttpContext. Fake implementation will not create real HttpContext for you.

Btw. where do you access the session outside controller? How do you know that there will be any current HttpContext?

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I'll get the HttpContext.Current.Session (which i check for null of course). But in unit test scenario's i'll get stuck. – Michel Nov 23 '10 at 19:40