[rspec-users] Possible improvements to routing spec API

Lalish-Menagh, Trevor trev at trevreport.org
Mon Jul 5 15:59:34 EDT 2010


OK, I will chime in here, since I think I might have opened up this
can of worms. :)

I agree with David that we should stick with wrapping the Rails public
APIs. That is: assert_generates, assert_recognizes, and assert_routing
(http://edgeapi.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html).

OK, here is my thoughts on a format:
http://gist.github.com/464636

I want it to be readable in English, so let's try:
describe "routing" do
  specify "days#index" do
    route get('/days').should generate 'days#index' # wraps assert_generates
  end
end

specify [that the] route [path] get('/days') should generate [the
options] 'days#index'

OK! Next:
describe "routing" do
  specify "days#index" do
    routing 'days#index'.should recognize get('/days') # wraps assert_recognizes
  end
end

specify [that the] route [options] 'days#index' should recognize [the
path] get('/days')

(Might need some work... but it is uniform). Next:
describe "routing" do
  specify "days#index" do
    route get('/days').should be 'days#index' # wraps assert_routing
  end
end

specify [that the] route [path] get('/days') should be [the options]
'days#index'

Finally, this makes nested routes (my original issue) look nice:
describe "routing" do
  specify "days#index" do
    route 'days#index'.should recognize get('/students/1/days') with
:student_id => '1'
  end
end

specify [that the] route [options] 'days#index' should recognize [the
path] get('/students/1/days') with [extras] :student_id => '1'

You could also write out the options longhand to include the extras,
but I think this is too ugly:
describe "routing" do
  specify "days#index" do
    route {:controller => 'days', :action => 'index', :student_id =>
'1'}.should recognize ('/students/1/days')
  end
end

specify [that the] route [options] {:controller => 'days', :action =>
'index', :student_id => '1'} should recognize [the path]
('/students/1/days')

What do you think?

Trevor


More information about the rspec-users mailing list