【发布时间】:2014-10-02 09:23:09
【问题描述】:
所以我在测试 ruby 类时遇到了这种奇怪的行为。顺便用rspec 3来测试一下。
类 Foo 有一个方法 'fetch_object',它从类 Bar 中调用 'find' 方法来检索对象,然后从获取的对象中调用方法 'fail'。
当我希望收到方法“失败”一次但没有收到时,就会发生所谓的奇怪行为,但是如果我将方法名称更改为“失败”,它就像一个魅力:S
剧情如下:
require 'ostruct'
class Foo
def fetch_object
foobar = Bar.find
foobar.fail
end
end
class Bar
def self.find
OpenStruct.new(name: 'Foo Bar')
end
end
describe Foo do
subject { Foo.new }
let(:foo) { OpenStruct.new() }
before do
expect(Bar).to receive(:find).and_return(foo)
end
it 'fetch object with name' do
expect(foo).to receive(:fail)
subject.fetch_object
end
end
【问题讨论】:
-
当我运行你的代码时,我得到
Failure/Error: expect(Bar).to receive(:find).and_return(foo) (<Bar (class)>).find(any args) expected: 1 time with any arguments received: 0 times with any arguments