title: How to mock the Request.ServerVariables using MOQ for ASP.NET MVC?
body: <p>i'm just learning to put in unit testing for my asp.net mvc when i came to learn about the mock and the different frameworks there is out there now.</p>
  
  <p>after checking SO, i found that MOQ seems to be the easiest to pick up.
  as of now i'm stuck trying to mock the Request.ServerVariables, as <a href="https://stackoverflow.com/questions/975276/asp-net-mvc-when-should-i-read-servervariables">after reading this post</a>, i've learned that it's better to abstract them into property.</p>
  
  <p>as such:</p>
  
  <pre><code>/// &lt;summary&gt;
          /// Return the server port
          /// &lt;/summary&gt;
          protected string ServerPort
          {
              get
              {
                  return Request.ServerVariables.Get("SERVER_PORT");
              }
          }
  </code></pre>
  
  <p>But i'm having a hard time learning how to properly mock this.
  I have a home controller ActionResult function which grabs the user server information and proceed to create a form to grab the user's information.</p>
  
  <p>i tried to use <a href="http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx" rel="nofollow noreferrer">hanselman's mvcmockhelpers class</a> but i'm not sure how to use it.</p>
  
  <p>this is what i have so far...</p>
  
  <pre><code>[Test]
          public void Create_Redirects_To_ProductAdded_On_Success() 
          {
  
              FakeViewEngine engine = new FakeViewEngine();
  
              HomeController controller = new HomeController();
              controller.ViewEngine = engine;
  
              MvcMockHelpers.SetFakeControllerContext(controller);
  
              controller.Create();
  
              var results = controller.Create();
  
              var typedResults = results as RedirectToRouteResult;
  
              Assert.AreEqual("", typedResults.RouteValues["action"], "Wrong action");
              Assert.AreEqual("", typedResults.RouteValues["controller"], "Wrong controller");
          }
  </code></pre>
  
  <h2>Questions:</h2>
  
  <ol>
  <li>As of now i'm still getting null
  exception error when i'm running the
  test. So what am i missing here?</li>
  <li>And if i use the mvcmockhelpers
  class, how can i still call the
  request.verifyall function to ensure
  all the mocking are properly setup?</li>
  </ol>