Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking The RouteData Class in System.Web.Routing for MVC applications

I'm trying to test some application logic that is dependent on the Values property in ControllerContext.RouteData.

So far I have

// Arrange var httpContextMock = new Mock<HttpContextBase>(MockBehavior.Loose); var controllerMock = new Mock<ControllerBase>(MockBehavior.Loose); var routeDataMock = new Mock<RouteData>();  var wantedRouteValues = new Dictionary<string, string>(); wantedRouteValues.Add("key1", "value1"); var routeValues = new RouteValueDictionary(wantedRouteValues);  routeDataMock.SetupGet(r => r.Values).Returns(routeValues);  <=== Fails here  var controllerContext = new ControllerContext(httpContextMock.Object, routeDataMock.Object, controllerMock.Object); 

The unit test fails with: System.ArgumentException: Invalid setup on a non-overridable member: r => r.Values

Creating a fake RouteData doesn't work either as the constructor is RouteData(RouteBase,IRouteHandler).

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!

Any help on this would be most welcome.

like image 859
Magpie Avatar asked Jun 12 '09 11:06

Magpie


2 Answers

RouteData also has a constructor that takes no arguments. Simply create one and add the values to it that you want. No need to mock it when you can create one.

 var routeData = new RouteData();  routeData.Values.Add( "key1", "value1" );   var controllerContext = new ControllerContext(httpContextMock.Object, routeData, controllerMock.Object); 
like image 65
tvanfosson Avatar answered Oct 13 '22 21:10

tvanfosson


I'm very new to TDD in conjunction with mock objects, but a lesson I learned early on from a colleague was not to mock types you don't own. Thus, don't try to mock RouteData. The idea was originally conceived by Joe Walnes (though I can't find where he said it).

like image 40
John Berberich Avatar answered Oct 13 '22 19:10

John Berberich