title: Unit testing routing/controller in MVC4
body: <p>Goal: test that a given url returns a given controller function.</p>
  
  <p>In the process, I've broken into the routing system and I can't figure out how to test routes (or, for that matter, find the controller corresponding to the route :-/).</p>
  
  <p>Sample code, which doesn't work:</p>
  
  <pre><code>[Test]       
  public void kick_the_tires()
  {
     var rc = new RouteCollection();
  
     Infrastructure.RouteRegistry.RegisterRoutes(rc);
  
     // get the route corresponding to name.
     var got = rc["name"];
  
     var expected = //What? foo is an internal type that can't be instantiated.
  
     Assert.AreEqual(foo, frob);
  }
  </code></pre>
  
  <hr>
  
  <p>edit: Using the linked blog post from Simon for the stub class.</p>
  
  <pre><code>[TestCase("/", "~/", "Home", "Index")]
  [TestCase("/", "api/command", "Other", "command")]
  internal void stub_mocker(string apppath, string route, string expected_controller,\
    string expected_action)
  {
    var rc = new RouteCollection();
    Infrastructure.RouteRegistry.RegisterRoutes(rc);
  
    var httpmock = new StubHttpContextForRouting(
        appPath: apppath,
        requestUrl: route);
  
    // this always returns null for everything but the Index case.
    var routeData = rc.GetRouteData(httpmock);
  
    var controller = routeData.Values["controller"];
    var action = routeData.Values["action"];
    Assert.AreEqual(expected_controller, controller);
    Assert.AreEqual(expected_action, action);
  }
  </code></pre>