Permalink
Browse files

Use .to_param instead of .id in controller spec templates.

Action Pack uses to_param for constructing an URL to objects. The default implementation returns this record’s id as a String. This way even if to_param is overridden in an ActiveRecord object, the controller spec templates are going to still work.
  • Loading branch information...
1 parent 4a0a855 commit 412b38a5a57241db1fc4da7ae2c06f6a5d361126 @attilagyorffy attilagyorffy committed Oct 20, 2011
Showing with 9 additions and 9 deletions.
  1. +9 −9 lib/generators/rspec/scaffold/templates/controller_spec.rb
View
18 lib/generators/rspec/scaffold/templates/controller_spec.rb
@@ -40,7 +40,7 @@ def valid_attributes
describe "GET show" do
it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
- get :show, :id => <%= file_name %>.id
+ get :show, :id => <%= file_name %>.to_param
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
end
@@ -55,7 +55,7 @@ def valid_attributes
describe "GET edit" do
it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
- get :edit, :id => <%= file_name %>.id
+ get :edit, :id => <%= file_name %>.to_param
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
end
@@ -106,18 +106,18 @@ def valid_attributes
# receives the :update_attributes message with whatever params are
# submitted in the request.
<%= class_name %>.any_instance.should_receive(:update_attributes).with(<%= params %>)
- put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => <%= params %>
+ put :update, :id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= params %>
end
it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
- put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => valid_attributes
+ put :update, :id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
it "redirects to the <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
- put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => valid_attributes
+ put :update, :id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes
response.should redirect_to(<%= file_name %>)
end
end
@@ -127,15 +127,15 @@ def valid_attributes
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
- put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => {}
+ put :update, :id => <%= file_name %>.to_param, :<%= ns_file_name %> => {}
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
it "re-renders the 'edit' template" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
- put :update, :id => <%= file_name %>.id, :<%= ns_file_name %> => {}
+ put :update, :id => <%= file_name %>.to_param, :<%= ns_file_name %> => {}
response.should render_template("edit")
end
end
@@ -145,13 +145,13 @@ def valid_attributes
it "destroys the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
- delete :destroy, :id => <%= file_name %>.id
+ delete :destroy, :id => <%= file_name %>.to_param
}.to change(<%= class_name %>, :count).by(-1)
end
it "redirects to the <%= table_name %> list" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
- delete :destroy, :id => <%= file_name %>.id
+ delete :destroy, :id => <%= file_name %>.to_param
response.should redirect_to(<%= index_helper %>_url)
end
end

0 comments on commit 412b38a

Please sign in to comment.