我尝试使用 RR 编写测试。我需要的是模型对象的存根。
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
我得到的是
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)>'
我不知道我做错了什么。我想我遵循文档...
您在此行的模型存根上调用方法“error_messages”:
stub(model).error_messages { messages }
我认为您实际上想在这里做其他事情,最有可能的是:
model.should_receive(:error_messages).and_return(messages)
它为 error_messages 创建一个存根方法,并在您的规范测试调用 model.error_messages 时使用您的消息数组进行响应
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)