Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Created June 21, 2011 17:50
Show Gist options
  • Save davidmfoley/1038439 to your computer and use it in GitHub Desktop.
Save davidmfoley/1038439 to your computer and use it in GitHub Desktop.
Good idea or bad idea?
#
# rspec shared examples for standard resource post to /create handling
#
# usage:
# it_should_behave_like "handles POST create"
#
# good idea or bad?
#
shared_examples_for 'handles POST create' do
before :all do
@attributes = {'foo' => 'bar'}
end
before :each do
@model_type_name ||= controller.class.name[0..-11].singularize
@underscored_name ||= @model_type_name.underscore
@model_type ||= @model_type_name.constantize
@model = mock_model(@model_type_name.constantize)
@model_type.stub(:new).with(@attributes) {@model}
@was_saved = false
end
describe "with valid params" do
before do
save_returns true
post_to_create
end
it "should create a new model" do
@was_saved.should == true
end
it "should assign the newly created model" do
assigns(@underscored_name).should be(@model)
end
it "should redirect to the newly created model" do
response.should redirect_to(@model)
end
end
describe "with invalid params" do
before do
save_returns false
post :create, @underscored_name => @attributes
end
it "assigns a newly created but unsaved property as @property" do
assigns(@underscored_name).should be @model
end
it "re-renders the 'new' template" do
response.should render_template("new")
end
end
def save_returns(result)
@model.stub(:save) {@was_saved = true; result}
end
def post_to_create
post :create, @underscored_name => @attributes
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment