About the author

Steven Harmansteven harman :: makes sweet software with computers!

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

News

Badges

  • Subtext Project
  • Support Subtext

A Little More Sugar for Testing Routes in asp.net MVC

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!

Technorati Tags: , , ,

kick it on DotNetKicks.com

What others are saying.

# re: A Little More Sugar for Testing Routes in asp.net MVC
Gravatar Ben Scheirman
Feb 05, 2009
Nice addition! Send the patch over.
# re: A Little More Sugar for Testing Routes in asp.net MVC
Gravatar Steven Harman
Feb 06, 2009
The patch has been submitted... do yer thang!
# re: A Little More Sugar for Testing Routes in asp.net MVC
Gravatar trendbender
Feb 07, 2009
thx for patch
# re: A Little More Sugar for Testing Routes in asp.net MVC
Gravatar Elliott O'Hara
Apr 20, 2009
See Bogards comments on flux88.com/.../fluent-route-testing-in-asp-net-mvc.

String.ShouldBeIgnored() seems really bad to me. It means nothing to anything but Route, so extend Route in some way.
Comments have been closed on this topic.