Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/524457/how-do-you-mock-the-session-object-collection-using-moq">Here</a> is a related discussion.</p> <p>I stopped referencing <code>HttpContext.Current</code> directly. and use this class instead:</p> <pre><code>public class HttpContextFactory { private static HttpContextBase m_context; public static HttpContextBase Current { get { if (m_context != null) return m_context; if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext not available"); return new HttpContextWrapper(HttpContext.Current); } } public static void SetCurrentContext(HttpContextBase context) { m_context = context; } } </code></pre> <p>and use <code>HttpContextFactory.Current</code> instead of <code>HttpContext.Current</code> in our code. </p> <p>Then you write this in your test:</p> <pre><code> HttpContextFactory.SetCurrentContext(GetMockedHttpContext()); </code></pre> <p>where GetMockedHttpContext() is from <a href="http://www.emadibrahim.com/2008/04/04/unit-test-linq-to-sql-in-aspnet-mvc-with-moq/" rel="nofollow noreferrer">here</a> and looks like this:</p> <pre><code> private System.Web.HttpContextBase GetMockedHttpContext() { var context = new Mock&lt;HttpContextBase&gt;(); var request = new Mock&lt;HttpRequestBase&gt;(); var response = new Mock&lt;HttpResponseBase&gt;(); var session = new Mock&lt;HttpSessionStateBase&gt;(); var server = new Mock&lt;HttpServerUtilityBase&gt;(); var user = new Mock&lt;IPrincipal&gt;(); var identity = new Mock&lt;IIdentity&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.Object); context.Setup(ctx =&gt; ctx.Server).Returns(server.Object); context.Setup(ctx =&gt; ctx.User).Returns(user.Object); user.Setup(x =&gt; x.Identity).Returns(identity.Object); identity.Setup(id =&gt; id.IsAuthenticated).Returns(true); identity.Setup(id =&gt; id.Name).Returns("test"); return context.Object; } </code></pre> <p>It uses a <a href="https://stackoverflow.com/questions/64242/rhino-mocks-typemock-moq-or-nmock-which-one-do-you-use-and-why">mocking framework</a> called <a href="http://code.google.com/p/moq/" rel="nofollow noreferrer">moq</a></p> <p>In your test project you have to add a reference to <code>System.Web</code> and <code>System.Web.Abstractions</code>, where <code>HttpContextBase</code> is defined.</p>
    singulars
    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