Expect to Receive and Call to Original

In integration specs, it is preferable to call the original method when setting up an expectation on an object to receive an invocation of that method. This way, the method isn’t stubbed out but instead will still be invoked. Any downstream effects of calling that method won’t be hidden.

class Calculator
  def self.add(x, y)
    x + y
  end
end

Should be tested like this:

require 'calculator'

RSpec.describe "and_call_original" do
  it "responds as it normally would" do
    expect(Calculator).to receive(:add).and_call_original
    expect(Calculator.add(2, 3)).to eq(5)  # any bugs inside of #add won't be hidden
  end
end

This code example is taken from: Relish - Calling the original implementation

Written on April 8, 2016 by shahriyarnasir