使用FactoryGirl时Rspec测试失败

使用FactoryGirl时Rspec测试失败,rspec,factory-bot,Rspec,Factory Bot,我第一次与FactoryGirl合作,并为以下控制器代码设置了测试 # PUT method def chosen answer = Answer.find(params[:id]) if answer.update_attributes({:selected => true}) respond_to do |format| format.html { flash[:notice] = "Success"

我第一次与FactoryGirl合作,并为以下控制器代码设置了测试


# PUT method
 def chosen    
    answer = Answer.find(params[:id])    
    if answer.update_attributes({:selected => true})
      respond_to do |format|
        format.html {
          flash[:notice] = "Success"
          redirect_to question_url(answer.question)
        }

        format.js { render :text => "Success" }
      end     
    end
  end
我的规范是测试,以查看该方法是否会将答案的所选(所选:布尔)属性值更新为true


require 'spec_helper'

describe AnswersController do
  integrate_views
  before(:each) do        
    @user = Factory.create(:user, :id => 1)
    @answer = Factory.create(:answer, :id => 1)
    @question = Factory.create(:question, :id => 1)
    User.stub!(:find).and_return(@user)
    @answer.stub!(:question).and_return(@question)
  end

  it "should use AnswersController" do
    controller.should be_an_instance_of(AnswersController)
  end

  describe "GET '/chosen'" do
    describe "mark as chosen when no answer is chosen" do            

      it "should mark a given answer as chosen" do
        #@answer.should_receive(:update_attributes!).and_return(true)
        put :chosen, :id => @answer.id
        @answer.should be_selected
      end


    end    
  end
end

我发现我的更改在我测试之前就被回滚了。我的意思是update_属性确实会被调用,它会将select属性的值更新为true,但在我的测试中,它表示answer.selected字段不会更新


需要帮助吗?

尝试在
放置后将其添加到规范中:

@answer.reload
这将从数据库中获取列的当前值,并更新
@answer
的属性。它还返回对象,因此您可以保存一行并放置:

@answer.reload.should be_selected

很抱歉,我尝试了,但它仍然无法重新加载我的更改。例如,在一个更简单的场景中,例如一个简单的更新,我无法将更新的对象与预期值进行比较。我错过了一些基本的东西。我在Rails 2.3.4和Rails 3.0.1中都有这个问题,我的错误是,有一个冲突的存根。谢谢你。