private static RouteData TestRouteViaTestDouble(string url, object expectedValues) { // Arrange (obtain routing config + set up test context) RouteCollection routeConfig = new RouteCollection(); MvcApplication.RegisterRoutes(routeConfig); var mockHttpContext = new TestHttpContext(url); // Act (run the routing engine against this HttpContextBase) RouteData routeData = routeConfig.GetRouteData(mockHttpContext); // Assert Assert.IsNotNull(routeData.Route, "No route was matched"); var expectedDict = new RouteValueDictionary(expectedValues); foreach (var expectedVal in expectedDict) { if (expectedVal.Value == null) { Assert.IsNull(routeData.Values[expectedVal.Key]); } else { Assert.AreEqual(expectedVal.Value.ToString(), routeData.Values[expectedVal.Key].ToString()); } } return(routeData); // ... in case the caller wants to add any other assertions }
private static string GenerateUrlViaTestDouble(object values) { // Arrange (get the routing config and test context) RouteCollection routeConfig = new RouteCollection(); RegisterAllAreas(routeConfig); MvcApplication.RegisterRoutes(routeConfig); var testContext = new TestHttpContext(null); RequestContext context = new RequestContext(testContext, new RouteData()); // Act (generate a URL) return(UrlHelper.GenerateUrl(null, null, null, /* Explained below */ new RouteValueDictionary(values), routeConfig, context, true)); }