1

我想确保在测试期间的某个时候Foo.bar调用我的方法。true到目前为止,我只能断言反对第一次调用Foo.bar. 我需要反对任何电话

这是我到目前为止但不起作用的:

  expect(Foo).to receive(:bar).at_least(:once).with("true")

  Foo.bar("false")
  Foo.bar("false")
  Foo.bar("true")
  Foo.bar("false")

它首先失败了,Foo.bar因为“假”不符合我的“真”期望。由于在测试期间的某个Foo.bar("true")时刻被调用,您将如何重写它以通过?

4

1 回答 1

1

我认为在这种情况下,您需要执行我认为的方法存根等效于as_null_object

describe Foo
  describe 'testing .bar multiple times' do
    before do
      allow(Foo).to receive(:bar) # stub out message
    end

    it "can determine how many times it has been called with 'true'" do
      expect(Foo).to receive(:bar).at_least(:once).with("true")
      expect(Foo).to receive(:bar).at_most(:once).with("true")
      Foo.bar("false")
      Foo.bar("false")
      Foo.bar("true")
      Foo.bar("false")
    end
  end
end
于 2014-04-04T01:32:14.533 回答