title: Setting properties on a mocked HttpContextBase
body: <p>I'm working on an ASP.NET MVC application, and am trying to write some unit tests against controller actions, some of which manipulate properties on the HttpContext, such as Session, Request.Cookies, Response.Cookies, etc. I'm having some trouble figuring out how to "Arrange, Act, Assert"...I can see Arrange and Assert...but I'm having trouble figuring out how to "Act" on properties of a mocked HttpContextBase when all of its properties only have getters. I can't <em>set</em> anything on my mocked context from within my controller actions...so it doesn't seem very useful. I'm fairly new to mocking, so I'm sure there's something that I'm missing, but it seems logical to me that I should be able to create a mock object that I can use in the context of testing controller actions where I can actually <em>set</em> property values, and then later Assert that they're still what I set them to, or something like that. What am I missing?</p>
  
  <pre><code>    public static HttpContextBase GetMockHttpContext()
      {
          var requestCookies = new Mock&lt;HttpCookieCollection&gt;();
          var request = new Mock&lt;HttpRequestBase&gt;();
          request.Setup(r =&gt; r.Cookies).Returns(requestCookies.Object);
          request.Setup(r =&gt; r.Url).Returns(new Uri("http://example.org"));
  
          var responseCookies = new Mock&lt;HttpCookieCollection&gt;();
          var response = new Mock&lt;HttpResponseBase&gt;();
          response.Setup(r =&gt; r.Cookies).Returns(responseCookies.Object);
  
          var context = new Mock&lt;HttpContextBase&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(new Mock&lt;HttpSessionStateBase&gt;().Object);
          context.Setup(ctx =&gt; ctx.Server).Returns(new Mock&lt;HttpServerUtilityBase&gt;().Object);
          context.Setup(ctx =&gt; ctx.User).Returns(GetMockMembershipUser());
          context.Setup(ctx =&gt; ctx.User.Identity).Returns(context.Object.User.Identity);
          context.Setup(ctx =&gt; ctx.Response.Output).Returns(new StringWriter());
  
          return context.Object;
      }
  </code></pre>