DEV Community

Tomasz Połeć
Tomasz Połeć

Posted on

Testing rescue with RSpec

How to test rescue with RSpec? How to check returned value?

require 'rspec'

class Foo
  def self.call
    Bar.call('first')
  rescue StandardError
    Bar.call('second')
  end
end

class Bar
  def self.call(message)
    message
  end
end

RSpec.describe do
  context 'Bar raises error during first execution' do
    it 'retries call' do
      expect(Bar).to receive(:call).once.and_raise
      expect(Bar).to receive(:call).once.and_call_original

      expect(Foo.call).to eq('second')
    end
  end
end

Top comments (0)