subject takes a block that returns a remainder object.
What you want is:
describe 'test' do subject { lambda { raise 'an exception' } } it { should raise_exception } end
Edit: comment explanation
It:
describe 'test' do subject { foo } it { should blah_blah_blah } end
more or less equivalent
(foo).should blah_blah_blah
Now consider: without lambda this will become:
(raise 'an exception').should raise_exception
See here that an exception occurs when an object is evaluated (before it should called at all). While with lambda it becomes:
lambda { raise 'an exception' }.should raise_exception
Here, the subject is the lambda, which is evaluated only when the should call is evaluated (in the context where the exception will be caught).
While the “subject” is re-evaluated each time, it should still evaluate what you want to be called should .
source share