1
我想了解爲什麼這些測試的結果,第一個測試聲稱方法不存在,但是,第二個是。用RSpec發出存根
class Roll
def initialize
install if !installed?
end
def install; puts 'install'; end
end
describe Roll do
before do
class RollTestClass < Roll; end
RollTestClass.any_instance.stub(:install)
end
let(:roll_class) { RollTestClass }
let(:roll) { RollTestClass.new }
context 'when installed is true' do
before do
roll_class.any_instance.stub(:installed?).and_return(true)
end
it 'should not call install' do
expect(roll).to_not have_received(:install)
end
end
context 'when installed is false' do
before do
roll_class.any_instance.stub(:installed?).and_return(false)
end
it 'should call install' do
expect(roll).to have_received(:install)
end
end
end
這也是奇怪的錯誤說:expected to have received install
,但我認爲這是有可能從RSpec的DSL只是錯誤的反饋。但也許值得注意。
1) Roll when installed is true should not call install
Failure/Error: expect(roll).to_not have_received(:install)
#<RollTestClass:0x10f69ef78> expected to have received install, but that method has not been stubbed.
非常好!我決定發佈S.O.而不是在rspec-mocks github的問題,因爲我想我正在做一些非常規或不正確的事情。很高興知道它的rspec問題。 expect_any_instance_of策略有點尷尬,但它必須這樣做。謝謝! – brewster
快速附錄:如果你使用Mocha進行模擬,當使用'any_instance'時,它將「實際」 – Neal