chrishough chrishough - 4 months ago 9
Ruby Question

rails 4 rspec 3.4 raise_error spec not returning true

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?

Answer

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!