Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I started with Scott Hanselman's <a href="http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx" rel="noreferrer">MVCMockHelper</a>, added a small class and made the modifications shown below to allow the controller to use Session normally and the unit test to verify the values that were set by the controller.</p> <pre><code>/// &lt;summary&gt; /// A Class to allow simulation of SessionObject /// &lt;/summary&gt; public class MockHttpSession : HttpSessionStateBase { Dictionary&lt;string, object&gt; m_SessionStorage = new Dictionary&lt;string, object&gt;(); public override object this[string name] { get { return m_SessionStorage[name]; } set { m_SessionStorage[name] = value; } } } //In the MVCMockHelpers I modified the FakeHttpContext() method as shown below public static HttpContextBase FakeHttpContext() { var context = new Mock&lt;HttpContextBase&gt;(); var request = new Mock&lt;HttpRequestBase&gt;(); var response = new Mock&lt;HttpResponseBase&gt;(); var session = new MockHttpSession(); var server = new Mock&lt;HttpServerUtilityBase&gt;(); context.Setup(ctx =&gt; ctx.Request).Returns(request.Object); context.Setup(ctx =&gt; ctx.Response).Returns(response.Object); context.Setup(ctx =&gt; ctx.Session).Returns(session); context.Setup(ctx =&gt; ctx.Server).Returns(server.Object); return context.Object; } //Now in the unit test i can do AccountController acct = new AccountController(); acct.SetFakeControllerContext(); acct.SetBusinessObject(mockBO.Object); RedirectResult results = (RedirectResult)acct.LogOn(userName, password, rememberMe, returnUrl); Assert.AreEqual(returnUrl, results.Url); Assert.AreEqual(userName, acct.Session["txtUserName"]); Assert.IsNotNull(acct.Session["SessionGUID"]); </code></pre> <p>It's not perfect but it works enough for testing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload