Mocking HttpSessionState in ASP.net for nunit testing -


I found the HTPassState and ASP.NM I am trying to write a test for an asp.net application and wondering if it is possible to start the session and if so, how?

I currently use Rhino Mox and NewYet

Gilbert,

Maybe I'm too late for you, I am using MSPC, but I think these concepts are the same. I needed to duplicate many components of HTTPContacts in the controllers under test.

I started with these following classes to copy essential components (for my purposes) in HTTTPtexbase. I filled only the necessary pieces within the classes. Your needs will vary according to your requirement in the controller. Once you understand the pattern, it is easy to add as needed.

  Public class MockHttpContext: HttpContextBase {Private Readonly HttpRequestBase _request = new MockHttpRequest (); Private Readonly HttpServerUtilityBase _server = New MockHttpServerUtilityBase (); Private HTTP://stressbook_session = new MockHttpSession (); Public override HttpRequestBase request {get {return _request; }} Public override the HttpServerUtilityBase server {get {return_server; }} Public override HTTP session current status session {Get {Back _Sation; }}} Public class MockHttpRequest: HttpRequestBase {Private Uri _url = New Uri ("http: //www.mockrequest.moc/Controller/Action"); Override ur ur URL {Return _url; }}} Public class MockHttpServerUtilityBase: HttpServerUtilityBase {public override string UrlEncode (string S) {// return base.UrlEncode (S); Return s; // is not doing anything (this is just a mock)}} Public class MockHttp session: http: //stackoverflow.com/questions/524457/how-do-you- Http://stackoverflow.com/users/81730/ronnblack system from mock-the-session-object -collection-using-moq // Selection. Generic.Dictionary & lt; String, Object & gt; _sessionStorage = new system Selection. Generic.Dictionary & lt; String, Object & gt; (); Public override object this [string name] {get {return_sessionStorage [name]; } Set {_sessionStorage [name] = value; }} Add public override zero (string name, object value) {_sessionStorage [name] = value; }}   

Here's how I set up Controller Context to use Mox (MSPC). This is the setup for actual tests on contoller (test obtained from this class)

  public abstract class BlahBlahControllerContext {protected static BlahBlahController controller; Install References = () = & gt; {Controller = new BlahBlahController (); Controller Admin Resource = New Controlling () {Controller = Controller, Request Context = New Request Context (New MokHTP Contact (), New Routadata ())}}; }; }   

Here is a test (Specification in the MSpec world), which uses fake session:

  [topic ("account : Recover Password ")] Recover Public Category _password_displays_retrieve_password2_page_on_success: BlahBlahControllerContext {Static ActionResult Results; Stable recovery password model model; Install References = () = & gt; {Model = New Retrieve PasswordModel () {User Name = "Mike"}; }; Due to: (=) {Results = Controller. Recovered password (model); }; It should_return_a_RedirectToRouteResult = () = & gt; {Result.is_a_redirect_to_route_and (). Action_name () ShouldEqual ("RetrievePassword2"); }; This session_should_contain_UN_value = () = & gt; {Controller HttpContext.Session ["United Nations"]. Should be ("Mike"); }; This session_should_contain_PQ_value = () = & gt; {Controller Html ptxaxation ["pq"] Must be ("question"); }; }   

I know that this Rhino does not use Mox. I hope this shows the principles and readers should adopt it to their specific tools and methods.

Comments