I came across something weird today, that I thought I'd write down before I came across it again later. I was writing a test to not expect an error like this:

it 'does not raise an error' do
  expect{ foo.bar }.to_not raise_error
end

But that raises an error (wait, is this meta? probably not...) because rspec is expecting an argument after raise_error. So, you'd have to write it like this:

it 'does not raise a NoMethodError' do
  expect{ foo.bar }.to_not raise_error(NoMethodError)
end

But what if I don't want to specify the error I don't want to get? What if I just want to make sure no error is raised at all? Turns out, all you need to do is switch some words around, like so:

it 'does not raise an error' do
  expect{ foo.bar }.not_to raise_error
end

And that works! Am I the only one who thinks that's super cool/weird? Anyway, go forth and not expect errors!


Connect with me!

Github
Email
LinkedIn
Personal Website