ruby-on-rails-3.1 – RSpec:使用调用update_attributes的方法
发布时间:2020-12-17 02:35:05 所属栏目:百科 来源:网络整理
导读:RSpec新手在这里. 我正在尝试测试我的模型,它们的方法使用update_attributes来更新其他模型的值. 我确信这些值在数据库中持久存在,但它们没有传递规范. 但是,当我包含像@ user.reload这样的东西时,它可以工作. 我想知道我是不是做错了.具体来说,如何测试改变
RSpec新手在这里.
我正在尝试测试我的模型,它们的方法使用update_attributes来更新其他模型的值. 我确信这些值在数据库中持久存在,但它们没有传递规范. 但是,当我包含像@ user.reload这样的东西时,它可以工作. 我想知道我是不是做错了.具体来说,如何测试改变其他模型属性的模型? 更新代码: describe Practice do before(:each) do @user = build(:user) @user.stub!(:after_create) @user.save! company = create(:acme) course = build(:acme_company,:company => company) course.save! @practice = create(:practice,:user_id => @user.id,:topic => "Factors and Multiples" ) end describe "#marking_completed" do it "should calculate the points once the hard practice is done" do @practice.question_id = Question.find(:first,conditions: { diff: "2" }).id.to_s @practice.responses << Response.create!(:question_id => @practice.question_id,:marked_results => [true,true,true]) @practice.save! lambda { @practice.marking_completed @practice.reload # <-- Must add this,otherwise the code doesn't work }.should change { @practice.user.points }.by(20) end end 结束 在Practice.rb def marking_completed # Update user points user.add_points(100) self.completed = true self.save! 结束 在User.rb中 def add_points(points) self.points += points update_attributes(:points => self.points) 结束 解决方法
发生的事情是用户对象缓存在@practice变量上,因此在用户更新时需要重新加载.使用当前规范,您无法做到这一点,但您可能想要考虑一下您实际测试的内容.我觉得你的规范是针对Practice模型的,但是断言应该改变{@ practice.user.points} .by(20)真的描述了User的行为,这似乎很奇怪.
我个人会在你的规范中将实践和用户模型分开一些,并独立测试他们的行为. describe "#marking_completed" do before do @practice.question_id = Question.find(:first,conditions: { diff: "2" }).id.to_s @practice.responses.create!(:question_id => @practice.question_id,true]) @practice.save! end it "should calculate the points once the hard practice is done" do @practice.user.should_receive(:add_points).with(20).once @practice.marking_completed end end 然后我会为User模型添加一个单独的测试: describe User do it 'should add specified points to a user' do lambda { subject.add_points(100) }.should change { subject.points }.by(100) end end 另一种旁注是,不清楚Question.find返回什么,或者为什么用户的分数在你的测试中改变了20. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |