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

tom*_*nek 0 ruby rspec ruby-on-rails ruby-on-rails-3

我尝试在控制器中添加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
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

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

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

   (compared using ==)
Run Code Online (Sandbox Code Playgroud)

但是当我评论时:

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

规格通过.为什么?

apn*_*ing 5

很合乎逻辑.

当你这样做时:

answer.should_receive(:update_attributes).with("content" => "Changed content")
Run Code Online (Sandbox Code Playgroud)

update_attributes 没有解雇.

您可以使用and_call_original方法触发.见文档.