Chris Hough
Chris Hough

Reputation: 3558

raise_error spec not returning true in Rspec 3.4

I have the following class, that I am trying to write a spec for:

module IntegrationError
  class Error < StandardError; end
  class BadRequest < IntegrationError::Error; end
  class LogicProblem < IntegrationError::Error; end

  def raise_logic_error!(message)
    raise IntegrationError::LogicProblem, message
  rescue => e
    Rails.logger.error e.message
    e.backtrace.each do |line|
      Rails.logger.error line if line.include?('integrations')
    end
  end

  def raise_bad_request!(message)
    raise IntegrationError::BadRequest, message
  end

  def log_bad_request!(message)
    Rails.logger.info message
  end
end

with spec

RSpec.describe 'IntegrationError', type: :integration do
  let!(:klass) { Class.new { include IntegrationError } }

  describe '#log_bad_request!' do
    it 'logs it' do
      expect(klass.new.log_bad_request!('TESTME')).to be_truthy
    end
  end

  describe '#raise_bad_request!' do
    it 'raises it' do
      binding.pry
      expect(klass.new.raise_bad_request!('TESTME')).to raise_error 
    end
  end
end

the raise_bad_request test returns the error instead of true. Anyone have thoughts on how to write this better to it passes?

I'm using Rails 4 and Rspec 3.4.

Upvotes: 2

Views: 89

Answers (2)

treiff
treiff

Reputation: 1306

If I recall correctly, I believe you need to pass the expectation a block when your raising, like this:

describe '#raise_bad_request!' do
  it 'raises it' do
    binding.pry
    expect{klass.new.raise_bad_request!('TESTME')}.to raise_error 
  end
end

See docs here

Upvotes: 4

Jordan Owens
Jordan Owens

Reputation: 742

For the raise_error matcher you need to pass a block to expect instead of a value:

expect { klass.raise_bad_request!('TESTME') }.to raise_error

That should do it!

Upvotes: 3

Related Questions