Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec raise error not working as expected, need correct syntax

I'm testing a class raises an error

context 'with no office manager' do
      let(:expected_response) do
        {
            error: "No office manager"
        }
      end
      it 'returns an error' do
        expect{ subject.run }.to raise_exception(an_instance_of(StandardError).and having_attributes(expected_response))
      end
    end

Gets the failed test response:

expected Exception with an instance of StandardError and having attributes {:error => "No office manager"}, got #<StandardError: {:error=>"No office manager"}>

What's the correct syntax to make this pass?

like image 919
Falko Avatar asked Oct 17 '25 14:10

Falko


2 Answers

I can say that's not so common to pass an object to an exception more than a string to hint the one that's reading it (or maybe also the caller). But if you want to check that, you can try passing a block to raise_error and expect the block argument message method is equal to the second argument for raise:

RSpec.describe 'raise plus a non-string second argument' do
  context 'with no office manager' do
    let(:expected_response) do
      { error: 'No office manager' }
    end

    it 'returns an error' do
      expect do
        raise StandardError, expected_response
      end.to raise_error(StandardError) { |error| expect(error.message).to eq expected_response.to_s }
    end
  end
end

Notice, raise_error and raise_exception are functionally interchangeable, so, you can use the one that makes the most sense to you in any given context.

like image 192
Sebastian Palma Avatar answered Oct 19 '25 08:10

Sebastian Palma


An exception in Ruby has a #message method/attribute, but not #error. So you need to change

let(:expected_response) do
  {
    error: "No office manager"
  }
end

to

let(:expected_response) do
  {
    message: "No office manager"
  }
end
like image 36
Viktor Avatar answered Oct 19 '25 09:10

Viktor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!