Unit Testing ASP.NET Routes

We are implementing routes. There are specific requirements around the tokens. We have 10 to 12 scenarios to test. Getting them wrong is not an option.

Using FakeItEasy (check it out if you have not, it’s a kick ass little mocking framework) here is the code I used to mock the routing.

Phil Haack has a great article on the subject.

    public abstract class RouteTestBase
    {
        /// 
        /// Gets the route data. Only pass in relative urls: ~/t/r/HeroRocksf9173ed1c882481fbe9681e8236fc98cu15zc4e3mhm/50006/42006_42006
        /// 
        /// The URL.
        /// 
        public RouteData GetRouteData(string url)
        {
            var virtualPathProvider = A.Fake();
            A.CallTo(() => virtualPathProvider.FileExists(A.Ignored)).Returns(false);

            var routes = new RouteCollection(virtualPathProvider);
            routes.Add(new Route(...));

            var contextBase = A.Fake();
            var request = contextBase.Request;

            A.CallTo(() => request.AppRelativeCurrentExecutionFilePath).Returns(url);

            return routes.GetRouteData(contextBase);
        }
    }