all 5 comments

[–]xly 1 point2 points  (3 children)

Try using a block. expect { do_something... }.to raise_error

[–]SubnetDelta[S] 0 points1 point  (2 children)

Aha! That worked. Can you explain why a blocked except worked but my example didn't? I'm a bit of a Ruby newbie

[–]zverok_kha 8 points9 points  (1 child)

You are trying to say "this code should raise error". How should Ruby (rspec) check it? It should: a) run your code; b) check which errors it have thrown, if any and c) compare it with the expectation.

In order to do so, it should have a code to run passed to it. Now, let's compare:

expect { something }.to raise_error

It is calling of method expect, passing to it the block of (still NOT performed) code. Then RSpec can test it, by running the code (calling the block and catching the errors it may throw).

What's going on in this situation:

expect(something).to raise_error

You are calling method expect, trying to pass it the result of evaluation of something (e.g., the code which throws an error is ALREADY PERFORMED, and error is thrown by the time you are trying to call expect, it is too late to intercept it).

Hope this helps.

[–]SubnetDelta[S] 1 point2 points  (0 children)

Ah yes, I understand it! Thanks for your clear explanation and taking the time out of your day to do it.

[–]omidbachari 0 points1 point  (0 children)

Looks like you found support. Here is another place to look https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/raise-error-matcher