title: MVC 4 Mocking HttpContext - how to mock DisplayModeProvider
body: <p>I was currently using <a href="http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx" rel="noreferrer">Scott Hanselmans HTTP context mock</a> for unit testing.  This worked well for MVC 3 and never looked back, I used it for testing calls for the following code.</p>
  
  <pre><code>public class PartialViewRenderer : IPartialViewRenderer
  {
      public string Render(Controller controller, string viewName, object model)
      {
          if (string.IsNullOrEmpty(viewName))
              viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
  
          controller.ViewData.Model = model;
  
          using (StringWriter sw = new StringWriter())
          {
              ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
              ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View,
                                                        controller.ViewData, controller.TempData, sw);
              viewResult.View.Render(viewContext, sw);
  
              return sw.GetStringBuilder().ToString();
          }
      }
  }
  </code></pre>
  
  <p>When I first converted my app to MVC 4 I ran into problems, it was getting runtime exceptions.  So I tried going through and fixing what needed fixing, which landed me on changing the following methods on Hanselmans MockHelpers: (I basically changed the HttpContext.Items to get returned since it was blowing an exception of "null")</p>
  
  <pre><code> public static HttpContextBase FakeHttpContext()
      {
          var context = new Mock&lt;HttpContextBase&gt;();
          var request = new Mock&lt;HttpRequestBase&gt;();
          var response = new Mock&lt;HttpResponseBase&gt;();
          var session = new Mock&lt;HttpSessionStateBase&gt;();
          var server = new Mock&lt;HttpServerUtilityBase&gt;();
          var cookies = new HttpCookieCollection();
          var items = new ListDictionary();
  
          request.Setup(r =&gt; r.Cookies).Returns(cookies);
          response.Setup(r =&gt; r.Cookies).Returns(cookies);
  
          context.Setup(ctx =&gt; ctx.Items).Returns(items);
  
          context.SetupGet(ctx =&gt; ctx.Request).Returns(request.Object);
          context.SetupGet(ctx =&gt; ctx.Response).Returns(response.Object);
          context.SetupGet(ctx =&gt; ctx.Session).Returns(session.Object);
          context.SetupGet(ctx =&gt; ctx.Server).Returns(server.Object);
  
          return context.Object;
      }
  
   public static void SetFakeControllerContext(this Controller controller, RouteData route)
      {
          var httpContext = FakeHttpContext();
  
          ControllerContext context = new ControllerContext(new RequestContext(httpContext, route), controller);
  
          controller.ControllerContext = context;
      }
  </code></pre>
  
  <p>Here a very simple nUnit test I have to try and nail down what changes I need to make to this mock of http context (I havent even put in the asserts yet)</p>
  
  <pre><code>    [Test]
      public void test()
      {
          _contactsController = _container.Resolve&lt;ContactsController&gt;();
  
          var route = new RouteData();
  
          route.Values.Add("controller", "ContactsController");
          route.Values.Add("action", "GetEditContactDetailsDialog");
  
          _contactsController.SetFakeControllerContext(route);
  
          var result = _contactsController.GetEditContactDetailsDialog("1");
      }
  </code></pre>
  
  <p>Now when I run this test it bombs on the PartialViewRenderer.Render call on line <code>ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);</code>  Here is the following stacktrace.</p>
  
  <blockquote>
    <p>at System.Web.WebPages.DisplayModeProvider.&lt;.ctor>b__2(HttpContextBase context)
       at System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext)
       at System.Web.WebPages.DisplayModeProvider.&lt;>c__DisplayClass6.b__5(IDisplayMode mode)
       at System.Linq.Enumerable.WhereListIterator<code>1.MoveNext()
       at System.Collections.Generic.List</code>1..ctor(IEnumerable<code>1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable</code>1 source)
       at System.Web.WebPages.DisplayModeProvider.GetAvailableDisplayModesForContext(HttpContextBase httpContext, IDisplayMode currentDisplayMode)
       at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]&amp; searchedLocations)</p>
  </blockquote>
  
  <p>It seems I cant get in there and mock the DisplayModeProvider.  <a href="http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/aec4b34a99c0#src/System.Web.WebPages/DefaultDisplayMode.cs" rel="noreferrer">Per the MVC source Code</a> Does anyone have a solution to this?  I havent been able to find a solution to this anywhere.</p>