title: what is Unit Testing MVC4 Response Status Code
body: <p>I have a unit test that test route with too many segments, but I'd like to extend it to test <code>Response Status Code</code> (which should return 404 code).</p>
  
  <p>Is it possible to use that mocked httpContext and check a status code?</p>
  
  <p><code>HttpContextMock.Object.Response.StatusCode</code> always returns 0 status code (due to mocking just one part of <code>HttpContext</code> I suppose).</p>
  
  <pre><code>[TestMethod]
  public void RouteWithTooManySegments()
  {
      var routes = new RouteCollection();
      MyApp.RouteConfig.RegisterRoutes(routes);
  
      var httpContextMock = new Mock&lt;HttpContextBase&gt;();
      var httpResponseMock = new Mock&lt;HttpResponseBase&gt;();
  
      httpContextMock.Setup(c =&gt; c.Request.AppRelativeCurrentExecutionFilePath)
          .Returns("~/dawd/dawdaw/dawdaw/daw");
  
      RouteData routeData = routes.GetRouteData(httpContextMock.Object);
  
      Assert.IsNull(routeData);
  }
  </code></pre>
  
  <p>Using <code>ErrorController</code> to return status code "404" in the view and appropriate <code>Response Status Code</code>:</p>
  
  <pre><code>public class ErrorController : Controller
  {
      public ActionResult Index()
      {
          return View();
      }
  
      public ActionResult NotFound()
      {
          Response.StatusCode = 404;
          return View();
      }
  }
  </code></pre>
  
  <p>and <code>Web.Config</code> rules</p>
  
  <pre><code> &lt;httpErrors errorMode="Custom" existingResponse="Replace"&gt;
    &lt;remove statusCode="404" /&gt;
    &lt;error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" /&gt;
   &lt;/httpErrors&gt;
  </code></pre>