Created
September 15, 2009 12:29
-
-
Save dchelimsky/187258 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# all of these variations will pass the accompanying spec using stubble | |
class ThingsController | |
def create | |
@thing = Thing.new(params[:thing]) | |
if @thing.save | |
render :action => 'index' | |
else | |
render :action => 'new' | |
end | |
end | |
end | |
#OR | |
class ThingsController | |
def create | |
@thing = Thing.create(params[:thing]) | |
if @thing.valid? | |
render :action => 'index' | |
else | |
render :action => 'new' | |
end | |
end | |
end | |
#OR | |
class ThingsController | |
def create | |
@thing = Thing.new(params[:thing]) | |
begin | |
@thing.save! | |
render :action => 'index' | |
rescue ActiveRecord::RecordInvalid | |
render :action => 'new' | |
end | |
end | |
end | |
#OR (even though you would never do this) | |
class ThingsController | |
def create | |
begin | |
@thing = Thing.create!(params[:thing]) | |
render :action => 'index' | |
rescue ActiveRecord::RecordInvalid | |
@thing = Thing.new(params[:thing]) | |
render :action => 'new' | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# stubble lets you express controller specs like this. it gives you | |
# the right feedback every step of the way, telling you when the model | |
# is not being accessed at all and when it receives messages with the | |
# wrong arguments, but it does not bind you to particular patterns. | |
describe ThingsController do | |
describe "POST create" do | |
it "creates a new post" do | |
stubbing(Thing, :params => {'these' => 'params'}) do | |
post :create, :thing => {'these' => 'params'} | |
end | |
end | |
it "redirects to index" do | |
stubbing(Thing) do | |
post :create | |
response.should render_template('index') | |
end | |
end | |
context "with invalid attributes" do | |
it "re-renders the 'new' template" do | |
stubbing(Thing, :as => :invalid) do | |
post :create | |
response.should render_template('new') | |
end | |
end | |
it "assigns the newly created thing" do | |
stubbing(Thing, :as => :invalid) do |thing| | |
post :create | |
assigns[:thing].should == thing | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment