title: Mocking The RouteData Class in System.Web.Routing for MVC applications
body: <p>I'm trying to test some application logic that is dependent on the Values property in  ControllerContext.RouteData.  </p>
  
  <p>So far I have</p>
  
  <pre><code>// Arrange
  var httpContextMock = new Mock&lt;HttpContextBase&gt;(MockBehavior.Loose);
  var controllerMock = new Mock&lt;ControllerBase&gt;(MockBehavior.Loose);
  var routeDataMock = new Mock&lt;RouteData&gt;();
  
  var wantedRouteValues = new Dictionary&lt;string, string&gt;();
  wantedRouteValues.Add("key1", "value1");
  var routeValues = new RouteValueDictionary(wantedRouteValues);
  
  routeDataMock.SetupGet(r =&gt; r.Values).Returns(routeValues);  &lt;=== Fails here
  
  var controllerContext = new ControllerContext(httpContextMock.Object, routeDataMock.Object, controllerMock.Object);
  </code></pre>
  
  <p>The unit test fails with:
  System.ArgumentException: Invalid setup on a non-overridable member:
  r => r.Values</p>
  
  <p>Creating a fake RouteData doesn't work either as the constructor is RouteData(RouteBase,IRouteHandler).</p>
  
  <p>The important class here is the abstract class RouteBase which has the method GetRouteData(HttpContextBase) which returns an instance of RouteData, the class I'm trying to fake. Taking me around in circles!</p>
  
  <p>Any help on this would be most welcome.</p>