title: Unit test dependencies for an ASP.NET MVC 3 ViewResult subclass
body: <p>I'm creating a really simple <code>ViewResult</code> subclass called <code>JavaScriptViewResult</code> that, when executing, calls the base implementation and then sets the <code>Content-Type</code> of the response to <code>text/javascript</code>.  In trying to unit test this class, I'm running across a slew of difficulties fulfilling all of the dependencies of the ASP.NET MVC stack.</p>
  
  <p>Here is what my unit test, which uses Rhino, looks like so far:</p>
  
  <pre><code>[TestMethod]
  public void TestExecuteAction()
  {
      var request = MockRepository.GenerateMock&lt;HttpRequestBase&gt;();
      request.Expect(m =&gt; m.Url).Return(new Uri("/Test/JavaScript", UriKind.Relative));
  
      var httpContext = MockRepository.GenerateMock&lt;HttpContextBase&gt;();
      httpContext.Expect(m =&gt; m.Request).Return(request);
  
      var controller = MockRepository.GenerateMock&lt;ControllerBase&gt;();
  
      var virtualPathProvider = MockRepository.GenerateMock&lt;VirtualPathProvider&gt;();
  
      var routeCollection = new RouteCollection(virtualPathProvider);
      routeCollection.MapRoute("FakeRoute", "Test/JavaScript", new { controller = "Test", action = "JavaScript" });
      var routeData = routeCollection.GetRouteData(httpContext);
  
      var context = new ControllerContext(httpContext, routeData, controller);
  
      var viewResult = new JavaScriptViewResult();
      viewResult.ExecuteResult(context);
  
      Assert.AreEqual("text/javascript", context.HttpContext.Response.ContentType);
  }
  </code></pre>
  
  <p>The latest exception when running the test is a <code>NullReferenceException</code> deep within the bowels of <code>System.Web.Routing.Route.GetRouteData(HttpContextBase httpContext)</code>.</p>
  
  <p>How do I set up all of the dependencies for executing a <code>ViewResult</code>?  Are there any techniques for making this simpler?  Alternately, is there a different way I can utilize the MVC view engine to generate JavaScript that will set the proper <code>Content-Type</code> for the response?</p>