Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing Resources Custom Matcher #1630

Closed
allcentury opened this issue May 30, 2016 · 1 comment
Closed

Routing Resources Custom Matcher #1630

allcentury opened this issue May 30, 2016 · 1 comment

Comments

@allcentury
Copy link

Testing resources in rails via routing specs in RSpec can be verbose.

For an entire resource, that means tests for all 7 actions:

it 'get to show' do
  expect({:get=>"admin/posts/1"}).to route_to({:controller=>"admin/posts", :action=>"show", :id=>"1"})
end
it 'get to index' do
  expect({:get=>"admin/posts"}).to route_to({:controller=>"admin/posts", :action=>"index"})
end
it 'get to new' do
  expect({:get=>"admin/posts/new"}).to route_to({:controller=>"admin/posts", :action=>"new"})
end
it 'get to edit' do
  expect({:get=>"admin/posts/1/edit"}).to route_to({:controller=>"admin/posts", :action=>"edit", :id=>"1"})
end
it 'post to create' do
  expect({:post=>"admin/posts"}).to route_to({:controller=>"admin/posts", :action=>"create"})
end
it 'delete to destroy' do
  expect({:delete=>"admin/posts/1"}).to route_to({:controller=>"admin/posts", :action=>"destroy", :id=>"1"})
end
it 'patch to update' do
  expect({:patch=>"admin/posts/1"}).to route_to({:controller=>"admin/posts", :action=>"update", :id=>"1"})
end

I have a few custom matchers to help with this, currently the DSL is:

expect("admin/posts").to have_resourceful_routes

or

expect("users").to have_resourceful_routes(:shallow)

Would the RSpec team be interested in those matchers or is the use case here too specific?

@allcentury allcentury changed the title Routing Resources Helper Routing Resources Custom Matcher May 30, 2016
@JonRowe
Copy link
Member

JonRowe commented May 31, 2016

You could implement and share these as a gem if you found others were interested, however it's not something we'd bring into rspec-rails. rspec-rails is a pretty thin wrapper around the test helpers rails provides, if they were to bring this into their helpers (probably as assert_resourceful_routes) we'd similarly then expose it.

As an aside however this isn't particularly useful testing, you're asserting that rails is doing it's job with 20 LOC in your tests for 1 LOC in your router, generally things like that don't need to be continually tested, it just adds to your build time with little chance of going wrong after it's committed.

@JonRowe JonRowe closed this as completed May 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants