rspec希望收到不工作

时间:2016-11-15 20:11:23

标签: ruby-on-rails rspec

我有以下类结构:

class A
  def process(processor)
    processor.process_x
  end
end

class Processor
  def process_x
  end
end

以及以下测试:

context "when process is called" do
  object_a = A.new
  processor = Processor.new

  it "calls processor.process_x" do
    object_a.process(processor)
    expect(processor).to receive(:process_x)
  end
end

但测试仍然失败。我甚至放了byebug并看到处理器的process_x方法被称为

我该怎么解决这个问题?

1 个答案:

答案 0 :(得分:6)

我看到了我的问题,在阅读this后,我意识到我应该首先添加期望,然后调用方法:

context "when process is called" do
  object_a = A.new
  processor = Processor.new

  it "calls processor.process_x" do
    expect(processor).to receive(:process_x) *** HERE
    object_a.process(processor) *** HERE
  end
end