【问题标题】:How to test only one of multiple method calls in RSpec?如何仅测试 RSpec 中的多个方法调用之一?
【发布时间】:2020-08-12 03:22:48
【问题描述】:
def some_method
  subject.put(1)
  subject.put(2)
  ...
end

以下失败,因为对put 的调用不止一次,是否可以仅验证第一次调用发生而不关心其余的调用?

expect(subject).to receive(:put).with(1).once

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    玩了一会儿,下面的工作。

    allow(subject).to receive(:put)
    expect(subject).to receive(:put).with(1).once
    

    【讨论】:

      【解决方案2】:

      通常你会像这样分开设置和期望:

      before do
        allow(subject).to receive(:put)
      end
      
      it 'invokes put' do
        expect(subject).to receive(:put).with(1).once
      end
      

      【讨论】:

        猜你喜欢
        • 2019-06-08
        • 2014-02-11
        • 1970-01-01
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多