imajes (owner)

Revisions

gist: 105884 Download_button fork
public
Public Clone URL: git://gist.github.com/105884.git
Embed All Files: show embed
assorted_implementations.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#These all pass the specs in post_create_with_stubble_spec.rb, but not necessarily the other ones
 
  def create
    @registration = Registration.new(params[:registration])
    if @registration.save
      redirect_to registrations_path
    else
      render :action => 'new'
    end
  end
 
  def create
    @registration = Registration.create(params[:registration])
    if @registration.valid?
      redirect_to registrations_path
    else
      render :action => 'new'
    end
  end
 
  def create
    begin
      @registration = Registration.create!(params[:registration])
      redirect_to registrations_path
    rescue ActiveRecord::RecordInvalid
      @registration = Registration.new(params[:registration])
      render :action => 'new'
    end
  end
 
 
post_create_spec.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  describe "POST create" do
    describe "with valid attributes" do
      it "redirects to list of registrations" do
        registration = stub_model(Registration)
        Registration.stub!(:new).and_return(registration)
        registration.stub!(:save!).and_return(true)
        post 'create'
        response.should redirect_to(registrations_path)
      end
    end
  
    describe "with invalid attributes" do
      it "re-renders the new form" do
        registration = stub_model(Registration)
        Registration.stub!(:new).and_return(registration)
        registration.stub!(:save!).and_raise(ActiveRecord::RecordInvalid.new(registration))
        post 'create'
        response.should render_template('new')
      end
    
      it "assigns the registration" do
        registration = stub_model(Registration)
        Registration.stub!(:new).and_return(registration)
        registration.stub!(:save!).and_raise(ActiveRecord::RecordInvalid.new(registration))
        post 'create'
        assigns[:registration].should equal(registration)
      end
    end
  end
 
post_create_with_singer_spec.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
when_staged(Registration) do
  
  describe "POST create" do
    describe "with valid attributes" do
      it "redirects to list of registrations" do
        perform.as(:successfull)
        post 'create'
        response.should redirect_to(registrations_path)
      end
    end
    
    describe "with invalid attributes" do
      it "re-renders the new form" do
        perform.as(:failure)
        post 'create'
        response.should render_template('new')
      end
      
      it "assigns the registration" do
        perform.as(:failure)
        post 'create'
        assigns[:registration].should equal(registration)
      end
    end
  end
end
post_create_with_stubble_spec.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  describe "POST create" do
    describe "with valid attributes" do
      it "redirects to list of registrations" do
        stubble(Registration)
        post 'create'
        response.should redirect_to(registrations_path)
      end
    end
    
    describe "with invalid attributes" do
      it "re-renders the new form" do
        stubble(Registration, :savable => false)
        post 'create'
        response.should render_template('new')
      end
      
      it "assigns the registration" do
        registration = stubble(Registration, :savable => false)
        post 'create'
        assigns[:registration].should equal(registration)
      end
    end
  end