[rspec-users] RSpec Routing DSL

Wincent Colaiuta win at wincent.com
Tue Oct 19 03:05:39 EDT 2010

El 18/10/2010, a las 20:20, Joe Fiorini escribió:

> I started testing routes for the first time in Rails 3 this weekend
> during Rails Rumble. I was so exhausted that I found writing route
> specs a very painful task. I came up with my own routing DSL and I'd
> love to see it get included in RSpec itself. Before I start adding the
> code to rspec-rails, I'd like to get some feedback and see if there
> are some ways we could clean it up. Basically the DSL looks like:
> describe "My routes" do
>  get "/blog" => { controller: "blogs-controller", action: "index" }
> end
> You can see all the details and the module used to make it work here:
> http://gist.github.com/630176. Thoughts?

I felt the same pain a while back and proposed a DSL too, but it never really got anywhere as there was no consensus about what a new DSL should look like. Full thread here:


I never got as far as submitting a patch because I didn't really like the alternative proposals so wasn't going to code them up (I'd already posted my own working proposal).

After several iterations, the implementation that I am currently using consists of "map_to", "map_from", "have_routing" (ie. map both ways) and "be_recognized" matchers; these were chosen largely because they don't clash with the existing matchers in RSpec and so I can use them on an "opt-in" basis:


Some sample specs: 



One thing to note is how there are two assertions in there where I use "map_to" instead of "have_routing" because of what looks to be a bug in the Rails routing assertion macros. I think there is a Lighthouse ticket for this but the only ones related to "assert_generates" which I can find right now are:


At least one of those issues (#5260, #5005) is supposedly resolved in 3.0.2. #5698 was marked as invalid. Tangentially related is this old ticket which I posted:


I thought someone posted a pretty good analysis of exactly what the breakage is and why it happens, but I can't find it. :-( Guess when I get time will have to do some analysis of the Rails codebase and figure out what's happening and put together another ticket.

While Googling, found this, however, describing changes in 2.0.0:


Which notes that RSpec's "route_to" now delegates to "assert_recognizes" (a one-way assertion) rather than "assert_routing" (a two-way assertion).



More information about the rspec-users mailing list