channa ly
channa ly

Reputation: 9927

Test Exception with Rspec Rails

I wanna do a test on a method call similar to:

call_to_method_1 param1

this method can raise for example:

raise msg1 if ...
raise msg2 if ...

My question is How to test exception with its msg1, msg2 exception messages wtih RSpec.

Upvotes: 0

Views: 951

Answers (1)

Tomek Wałkuski
Tomek Wałkuski

Reputation: 1009

describe SomeClass do
  let(:some_object) { described_class.new }

  it 'should raise an exception' do
    some_object.some_method('param').should raise_error(ExceptionClass, "exception_message")
  end
end

if you raise "message" then the ExceptionClass will be instance of RuntimeClass.

IMO it's better to raise specific type of exception instead of relying on message though.

Upvotes: 2

Related Questions