我无法模拟注入的对象。例如:
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)
有什么想法吗?
答案 0 :(得分:4)
mock_bar.should_receive(:do_something_cool).once
应该在
之前real_foo.run