In a codebase I work in, NHibernate needs an HttpContext to get its current session – like this:
public static ISession GetCurrentSession()
{
var context = HttpContext.Current;
var currentSession = context.Items[CurrentSessionKey] as ISession;
...
To get my hands on a session for a NUnit test – I put this in the setup:
HttpContext.Current = new HttpContext(
new HttpRequest(null, "http://tempuri.org", null),
new HttpResponse(null));
Also, I wipe the context in the teardown:
<code>
[TearDown]
public void TearDown()
{
HttpContext.Current = null;
}
</code>
Credit for this goes to Caio Proiete:
http://caioproiete.net/en/fake-mock-httpcontext-without-any-special-mocking-framework/