How to write an RSpec test for a simple PUT update?

前端 未结 4 1832
长情又很酷
长情又很酷 2020-12-08 19:36

I\'m trying to solidify my understanding of rails and the BDD workflow, so I wanted to start small by creating one of those mini-blogs, but with rspec. Right now I have an A

相关标签:
4条回答
  • 2020-12-08 20:13

    The way I like to test the update method is to just ensure that the updated_at time is greater than it was before. When you do this you can change the contents of the entire instance variable and still check if everything was updated. For instance:

    describe "PUT 'update/:id'" do
      it "allows an article to be updated" do
        prev_updated_at = @article.updated_at
        @attr = { :title => "new title", :content => "new content" }
        put :update, :id => @article.id, :article => @attr
        @article.reload
        @article.updated_at.should != prev_updated_at 
      end
    end
    
    0 讨论(0)
  • 2020-12-08 20:22

    You forgot to .reload your @article, and on update action your response most likely perform redirect, so

    RSpec 2:

    describe "PUT update/:id" do
      let(:attr) do 
        { :title => 'new title', :content => 'new content' }
      end
    
      before(:each) do
        put :update, :id => @article.id, :article => attr
        @article.reload
      end
    
      it { response.should redirect_to(@article) }
      it { @article.title.should eql attr[:title] }
      it { @article.content.should eql attr[:content] }
    end
    

    Rspec 3:

    describe "PUT update/:id" do
      let(:attr) do 
        { :title => 'new title', :content => 'new content' }
      end
    
      before(:each) do
        put :update, :id => @article.id, :article => attr
        @article.reload
      end
    
      it { expect(response).to redirect_to(@article) }
      it { expect(@article.title).to eql attr[:title] }
      it { expect(@article.content).to eql attr[:content] }
    end
    
    0 讨论(0)
  • 2020-12-08 20:28

    When you are doing a PUT :update remember that you are editing an existing model, which you need to call in the put. Just pass your @article and update the attributes as follows.

    describe "PUT 'update/:id'" do
      it "allows an article to be updated" do
        put :update, :id => @article.id, :article => @article.attributes = { :title => "new title", :content => "new content" }
        response.should be_successful
      end
    end
    
    0 讨论(0)
  • 2020-12-08 20:29
    FactoryGirl.define :article do
      title "a title"
      content "hello world"
    end
    
    before(:each) do
      @article = Factory(:article)
    end
    
    it "should re-render edit template on failed update" do
      @attr = { :title => "", :content => "new content" }
      put :update, :id => @article.id, :article => @attr
    
      flash[:notice].should be_nil
      response.should render_template('edit')
    end
    
    it "should redirect to index with a notice on successful update" do
      @attr = { :title => "new title", :content => "new content" }
      put :update, :id => @article.id, :article => @attr
    
      assigns[:article].should_not be_new_record
      flash[:notice].should_not be_nil
      response.should redirect_to(:action => 'index')
    end
    
    0 讨论(0)
提交回复
热议问题