【发布时间】:2012-05-01 22:41:57
【问题描述】:
我无法模拟注入的对象。例如:
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)
有什么想法吗?
【问题讨论】:
标签: ruby dependency-injection rspec mocking