title: How to mock the controller context with moq
body: <p>I am trying out the MOQ framework and up now I have hit a barrier.  The following unit test fails because the actual value of the ViewName property is an empty string.</p>
  
  <p>Could anyone point me in the right direction please as to why this is not passing the test?</p>
  
  <pre><code>[TestMethod]
  public void Can_Navigate_To_About_Page()
  {
      var request = new Mock&lt;HttpRequestBase&gt;();
      request.Setup(r =&gt; r.HttpMethod).Returns("GET");
      var mockHttpContext = new Mock&lt;HttpContextBase&gt;();
      mockHttpContext.Setup(c =&gt; c.Request).Returns(request.Object);
  
      var controllerContext = new ControllerContext(mockHttpContext.Object, 
                                  new RouteData(), 
                                  new Mock&lt;ControllerBase&gt;().Object);
      var controller = new HomeController();
  
      controller.ControllerContext = controllerContext;
      var result = controller.About() as ViewResult;
  
      Assert.AreEqual("About", result.ViewName);
  }
  </code></pre>
  
  <p>The following also yields an empty ViewName.  </p>
  
  <pre><code>        HomeController controller = new HomeController();
          ViewResult result = controller.About() as ViewResult;
          Assert.IsNotNull(result);
          Assert.AreEqual("About", result.ViewName);
  </code></pre>
  
  <p>From examples on the web which demonstrate mocking and also good TTD I am just confused as to what other plumbing I need to make either of the above first unit test example work.</p>
  
  <p>Cheers,</p>
  
  <p>Andrew</p>