1

我无法模拟注入的对象。例如:

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)

有任何想法吗?

4

1 回答 1

4
mock_bar.should_receive(:do_something_cool).once 

应该是之前

real_foo.run
于 2012-05-01T22:46:12.347 回答