Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restricted controllers tested with more readability

  • Loading branch information...
commit 7b377c15e914e0d4ee6334f3e2aa8f5df8c742a2 1 parent 8a0a6f5
@thejamespinto thejamespinto authored dchelimsky committed
Showing with 23 additions and 16 deletions.
  1. +23 −16 lib/generators/rspec/scaffold/templates/controller_spec.rb
View
39 lib/generators/rspec/scaffold/templates/controller_spec.rb
@@ -26,12 +26,19 @@
def valid_attributes
{}
end
+
+ # This should return the minimal values that should be in the session
+ # in order to pass any filters (such as authentication) required by
+ # <%= controller_class_name %>Controller, be sure to keep this updated too.
+ def valid_session
+ {}
+ end
<% unless options[:singleton] -%>
describe "GET index" do
it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
- get :index
+ get :index, {}, valid_session
assigns(:<%= table_name %>).should eq([<%= file_name %>])
end
end
@@ -40,14 +47,14 @@ 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 %>.to_param
+ get :show, {:id => <%= file_name %>.to_param}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
end
describe "GET new" do
it "assigns a new <%= ns_file_name %> as @<%= ns_file_name %>" do
- get :new
+ get :new, {}, valid_session
assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
end
end
@@ -55,7 +62,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 %>.to_param
+ get :edit, {:id => <%= file_name %>.to_param}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
end
@@ -64,18 +71,18 @@ def valid_attributes
describe "with valid params" do
it "creates a new <%= class_name %>" do
expect {
- post :create, :<%= ns_file_name %> => valid_attributes
+ post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
}.to change(<%= class_name %>, :count).by(1)
end
it "assigns a newly created <%= ns_file_name %> as @<%= ns_file_name %>" do
- post :create, :<%= ns_file_name %> => valid_attributes
+ post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
assigns(:<%= ns_file_name %>).should be_a(<%= class_name %>)
assigns(:<%= ns_file_name %>).should be_persisted
end
it "redirects to the created <%= ns_file_name %>" do
- post :create, :<%= ns_file_name %> => valid_attributes
+ post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
response.should redirect_to(<%= class_name %>.last)
end
end
@@ -84,14 +91,14 @@ def valid_attributes
it "assigns a newly created but unsaved <%= ns_file_name %> as @<%= ns_file_name %>" do
# Trigger the behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
- post :create, :<%= ns_file_name %> => {}
+ post :create, {:<%= ns_file_name %> => {}}, valid_session
assigns(:<%= ns_file_name %>).should be_a_new(<%= class_name %>)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
<%= class_name %>.any_instance.stub(:save).and_return(false)
- post :create, :<%= ns_file_name %> => {}
+ post :create, {:<%= ns_file_name %> => {}}, valid_session
response.should render_template("new")
end
end
@@ -106,18 +113,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 %>.to_param, :<%= ns_file_name %> => <%= params %>
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= params %>}, valid_session
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 %>.to_param, :<%= ns_file_name %> => valid_attributes
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
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 %>.to_param, :<%= ns_file_name %> => valid_attributes
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
response.should redirect_to(<%= file_name %>)
end
end
@@ -127,7 +134,7 @@ 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 %>.to_param, :<%= ns_file_name %> => {}
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => {}}, valid_session
assigns(:<%= ns_file_name %>).should eq(<%= file_name %>)
end
@@ -135,7 +142,7 @@ 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 %>.to_param, :<%= ns_file_name %> => {}
+ put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => {}}, valid_session
response.should render_template("edit")
end
end
@@ -145,13 +152,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 %>.to_param
+ delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
}.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 %>.to_param
+ delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
response.should redirect_to(<%= index_helper %>_url)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.