user1291365 user1291365 - 1 month ago 13
Ruby Question

rspec: raise_error usage to match error message

I raised an error using

raise(ConfigurationError.new(msg))


I tried to test this with rspec:

expect {
Base.configuration.username
}.to raise_error(ConfigurationError, message)


But this doesn't work. How can I test this? The goal is to match
message
.

Answer

Make sure you are using rspec > 2.14.0 and take a look at this commit:

https://github.com/rspec/rspec-expectations/commit/7f02b503d5ae48d1141b6465acd0a7a4e1bb84dd

it "passes if an error instance is expected" do
  s = StandardError.new
  expect {raise s}.to raise_error(s)
end
Comments