【问题标题】:Ensure method was called with args at some point during test确保在测试期间的某个时刻使用 args 调用方法
【发布时间】:2014-04-03 21:33:57
【问题描述】:

我想确保我的 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") 在测试期间的某个点被调用,您将如何重写它以通过?

【问题讨论】:

    标签: ruby rspec mocking expectations


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多