Skip to content

Instantly share code, notes, and snippets.

@blueplanet
Created July 21, 2014 06:04
Show Gist options
  • Save blueplanet/f595c8d551970c9b8f5c to your computer and use it in GitHub Desktop.
Save blueplanet/f595c8d551970c9b8f5c to your computer and use it in GitHub Desktop.
respond_with について調べてみた ref: http://qiita.com/blueplanet/items/9587b31f1805d80fd4c3
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Journal.any_instance.stub(:save).and_return(false)
post :create, {:journal => { "title" => "invalid title" }}, valid_session
response.should render_template(:new)
end
it "re-renders the 'edit' template" do
journal = Journal.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Journal.any_instance.stub(:save).and_return(false)
put :update, {:id => journal.to_param, :journal => { "title" => "invalid title" }}, valid_session
response.should render_template(:edit)
end
it "updates the requested journal" do
journal = Journal.create! valid_attributes
# Assuming there are no other journals in the database, this
# specifies that the Journal created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Journal.any_instance.should_receive(:update).with({ "title" => "MyString" })
put :update, {:id => journal.to_param, :journal => { "title" => "MyString" }}, valid_session
end
Failures:
1) JournalsController PUT update with invalid params re-renders the 'edit' template
Failure/Error: response.should render_template(:edit)
expecting <"edit"> but rendering with <[]>
# ./spec/controllers/journals_controller_spec.rb:122:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
2) JournalsController POST create with invalid params re-renders the 'new' template
Failure/Error: response.should render_template(:new)
expecting <"new"> but rendering with <[]>
# ./spec/controllers/journals_controller_spec.rb:78:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
3) JournalsController PUT update with valid params updates the requested journal
Failure/Error: Unable to find matching line from backtrace
Exactly one instance should have received the following message(s) but didn't: update
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
# Journal.any_instance.stub(:save).and_return(false)
post :create, {:journal => { "title" => nil }}, valid_session
response.should render_template(:new)
end
it "re-renders the 'edit' template" do
journal = Journal.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
# Journal.any_instance.stub(:save).and_return(false)
put :update, {:id => journal.to_param, :journal => { "title" => nil }}, valid_session
response.should render_template(:edit)
end
it "updates the requested journal" do
journal = Journal.create! valid_attributes
# Assuming there are no other journals in the database, this
# specifies that the Journal created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Journal.any_instance.should_receive(:update_attributes).with({ "title" => "MyString" })
put :update, {:id => journal.to_param, :journal => { "title" => "MyString" }}, valid_session
end
def navigation_behavior(error)
if get?
raise error
elsif has_errors? && default_action
render :action => default_action
else
redirect_to navigation_location
end
end
...
def has_errors?
resource.respond_to?(:errors) && !resource.errors.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment