所以我在測試Ruby類時遇到了這種奇怪的行爲。順便說一句,我使用rspec 3來測試它。Ruby + Rspec + OpenStruct怪異行爲
Foo類有一個方法'fetch_object',它調用類Bar中的'find'方法來檢索一個對象,並從獲取的對象中調用方法'fail'。
時,我期望得到法「失敗」一次收到沒有,但如果我爲「faill」改變方法的名稱它就像一個魅力的所謂怪異的行爲發生: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
時我運行你的代碼我得到了'失敗/錯誤:expect(Bar).to接收(:find).and_return(foo) ().find(任何參數) 預期: ts 收到:使用任何參數0次 –
Anthony
2014-10-02 00:33:06