The other day I was spec’ing out some new routes for my application and I wanted to make sure certain routes were ignored. Not happy with the disgustingly-verbose way of testing routes OOTB with asp.net MVC, I decided to lean on the great work Ben Schierman did in his fluent route testing extensions.
I whipped together a quick extension method that allows me to do the following:
1: public class when_requesting_a_gif_file : with_routes_configured
2: {
3: [Test]
4: public void should_ignore_the_route()
5: {
6: "~/my_file.gif".ShouldBeIgnored();
7: }
8: }
Building the extension was a snap – I leveraged the work already in place for the other fluent route testing extensions, and just made sure the RouteHandler responsible for the given route pattern was a StopRoutingHandler. It looks something like this:
1: public static class RouteTestingExtensions
2: {
3: public static RouteData ShouldBeIgnored(this string relativeUrl)
4: {
5: RouteData routeData = relativeUrl.Route();
6: routeData.RouteHandler.ShouldBeOfType<StopRoutingHandler>();
7:
8: return routeData;
9: }
10: }
And that's all there is to it!
I’ll be submitting I have submitted this as a patch to MVC Contrib project within the next few minutes. Hopefully I can coax one of the committers into applying it. But until then, just steal my code and roll it into your code base!
