Stub (…) received unexpected message (…) with (no args)

巧了我就是萌 提交于 2019-12-12 20:25:22

问题


I try to write a test using RR. What I need is a stub of a model object.

describe ApplicationController do

  subject(:application_controller)     { ApplicationController.new }
  let(:messages)                       { ['a1', 'a2', 'a3' ] }
  let(:model)                          { Object.new }

  it 'should copy errors to flash' do
    stub(model).error_messages { messages }
    flash[:error] == nil
    subject.copy_errors_to_flash(model)
    flash[:error].should == messages
  end

end

What I get is

ApplicationController should copy errors to flash
     Failure/Error: stub(model).error_messages { messages }
       Stub #<Object:0x007ffaa803f930> received unexpected message :error_messages with (no args)
     # ./spec/controllers/application_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

I have no idea what am I doing wrong. I think I follow the docs...


回答1:


You're calling the method 'error_messages on the stub of your model on this line:

stub(model).error_messages { messages }

I presume that you actually want to do something else here, most likely:

model.should_receive(:error_messages).and_return(messages)

which creates a stub method for error_messages and will respond with your messages array whenever your spec tests calls model.error_messages



来源:https://stackoverflow.com/questions/16034960/stub-received-unexpected-message-with-no-args

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!