title: How can I correctly mock my controllercontext to test ViewResult.ExecuteResult()?
body: <p>I am attempting to create integration tests to make sure my views do not have any runtime errors in them.   Thus I need to create a test that checks if <code>ViewResult.ExecuteResult()</code> works correctly but it seems I have hit a snag.  </p>
  
  <p>I found <a href="http://www.heartysoft.com/post/2010/05/25/ASPNET-MVC-Unit-Testing-JsonResult-Returning-Anonymous-Types.aspx" rel="noreferrer">this site</a> which gave me a starting point, and I have the following code:</p>
  
  <pre><code>    [TestMethod]
      public void RegisterResultExecutes()
      {
          //arrange 
          RequestContext requestContext = new RequestContext(new MockHttpContext(), new RouteData());
          AccountController controller = new AccountController
          {
              FormsService = new MockFormsAuthenticationService(),
              MembershipService = new MockMembershipService(),
              Url = new UrlHelper(requestContext)
          };
  
          var result = controller.Register();
          var sb = new StringBuilder();
          Mock&lt;HttpResponseBase&gt; response = new Mock&lt;HttpResponseBase&gt;();
          response.Setup(x =&gt; x.Write(It.IsAny&lt;string&gt;())).Callback&lt;string&gt;(y =&gt;
          {
              sb.Append(y);
          });
          Mock&lt;ControllerContext&gt; controllerContext = new Mock&lt;ControllerContext&gt;();
          controllerContext.Setup(x =&gt; x.HttpContext.Response).Returns(response.Object);
  
          //act 
          result.ExecuteResult(controllerContext.Object);
      }
  </code></pre>
  
  <p>The problem is that when <code>result.ExecuteResult()</code> is called I get the following exception</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  
  System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
  MyApp.Tests.Controllers.AccountControllerTest.RegisterResultExecutes() in C:\Users\KallDrexx\Documents\Projects\MyApp\MyApp.Tests\Controllers\AccountControllerTests.cs: line 297
  </code></pre>
  
  <p>Unfortunately, that stack trace isn't very useful as I'm not sure what it's trying to access that is null.  Does anyone have any suggestions on how I can create a test for <code>ExecuteResult()</code>?</p>