我无法模拟注入的对象。例如:
class Foo
def initialize(bar = Bar.new)
@bar = bar
end
def run
@bar.do_something_cool
end
end
# Rspec
describe Foo do
it "should do something cool" do
mock_bar = mock("bar")
mock_bar.stub(:do_something_cool).and_return(nil)
real_foo = Foo.new(mock_bar)
real_foo.run
mock_bar.should_receive(:do_something_cool).once
end
end
如果我运行它,规范会失败,因为它说永远不会调用“do_something_cool”。
expected: 1 time
received: 0 times
但是,如果我不存根“do_something_cool”,我会收到以下错误
Mock "bar" received unexpected message :do_something_cool with (no args)
有任何想法吗?