Access SessionState from within an NUnit test

nunitsessionsession-state

One of my methods in an NUnit test checks a Session-State variable :

if (Sesssion[variableName] == null) …

and the NUnit test is failing with err:

System.Web.HttpException: Session
state can only be used when
enableSessionState is set to true,
either in a configuration file or in
the Page directive. Please also make
sure that
System.Web.SessionStateModule or a
custom session state module is
included in the
\\
section in the application
configuration.

I tried taking the test method out of a class library and putting it into the codebehind cs of an aspx page that set EnableSessionState="True" in the aspx page directive. Added a web config with sessionState mode = "InProc" in the web config, then tried < pages enableSessionState ="true"/> in the web.config.

Nunit tests still failed on the session state action.

Do I have to mock these out?

Best Answer

I don't believe you can access Session from your unit tests without mocking it.

Have a look at Phil Haack's HttpSimulator http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx

Related Topic