所以我在测试 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