Has anyone mocked FriendlyUrls for unit testing?
I am writing a test that needs to mock asp.net FriendlyUrls. The call I need to mock specifically is Request.GetFriendlyUrlSegments(). I am using MS Fakes.
Here is my test so far:
// Arrange
var httpContext = TestHelper.StubHtppContext("", "http://localhost/content.aspx/area/controller/action/OtherRouteValue", "");
var httpContextBase = new HttpContextWrapper(httpContext);
RouteTable.Routes.MapRoute(
"RouteName",
"Area/{controller}/{action}/{id}/{OtherRoute}",
new {action = "Index", id = UrlParameter.Optional, OtherRoute = UrlParameter.Optional});
RouteTable.Routes.EnableFriendlyUrls();
var segments = new List<String> {"Controller", "Action", "Id", "OtherRoute"};
using (ShimsContext.Create())
{
ShimHttpContext.CurrentGet = () => httpContext;
ShimFriendlyUrl.SegmentsGet = () => segments;
// Act
RouteData result = MvcUtility.GetRouteValuesFromUrl();
// Assert
Assert.IsNotNull(result, "Expected RouteData to be created.");
}
}
The relevant Part of the system under test:
public static RouteData GetRouteValuesFromUrl()
{
var request = System.Web.HttpContext.Current.Request;
var segments = request.GetFriendlyUrlSegments();
//Other code
}
I would expect for segments to use my shim get and return my list of segments.
My code works when I run it in the web context, I just need to find a way to unit test it and the first step is mocking shim/stub this request.GetFriendlyUrlSegments() call.