为什么我不能使用should_receive并分配一个控制器规范?

时间:2013-03-20 13:28:08

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

我尝试将assigns添加到控制器

中的update操作规范中
it "update the content" do
  Answer.should_receive(:find).with(answer.id.to_s).and_return(answer)
  answer.should_receive(:update_attributes).with("content" => "Changed content")

  put :update, id: answer.id, app_id: app.id, answer: {content: "Changed content"}

  assigns(:answer).content.should eq('Changed content') # explicitly permitted
  response.status.should eq 406
end

但我收到错误:

 Failure/Error: assigns(:answer).content.should eq('Changed content') # explicitly permitted

   expected: "Changed content"
        got: "base content"

   (compared using ==)

但是当我评论时:

#answer.should_receive(:update_attributes).with("content" => "Changed content")

规格通过。为什么呢?

1 个答案:

答案 0 :(得分:5)

很合乎逻辑。

当你这样做时:

answer.should_receive(:update_attributes).with("content" => "Changed content")

update_attributes未被解雇。

您可以使用and_call_original来触发该方法。 See doc