Is there a way to properly test exception raising with implicit subjects in rspec?
For example, this fails:
describe 'test' do
subject {raise 'an exception'}
it {should raise_exception}
end
But this passes:
describe 'test' do
it "should raise an exception" do
lambda{raise 'an exception'}.should raise_exception
end
end
Why is this?
subjectaccepts a block which returns the subject of the remainder.What you want is this:
Edit: clarification from comment
This:
is more or less equivalent to
Now, consider: without the
lambda, this becomes:See here that the exception is raised when the subject is evaluated (before
shouldis called at all). Whereas with the lambda, it becomes:Here, the subject is the lambda, which is evaluated only when the
shouldcall is evaluated (in a context where the exception will be caught).While the “subject” is evaluated anew each time, it still has to evaluate to the thing you want to call
shouldon.