views:

383

answers:

2

Hi,

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.

+6  A: 

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);
tvanfosson
Lol, thank you. This was a case of over thinking the problem on my part. Very simple. Cheers.
Dominic Godin
Joy! thanks for this tvanfosson :)
Pure.Krome
A: 

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).

John Berberich