Block Matchers
With all the expectations we’ve seen so far, we’ve passed regular Ruby objects into expect:
| expect(3).to eq(3) |
This is fine for checking properties of your data. But sometimes you need to check properties of a piece of code. For example, perhaps a certain piece of code is supposed to raise an exception. In this case, you can pass a block into expect:
| expect { raise 'boom' }.to raise_error('boom') |
RSpec will run the block and watch for the specific side effects you specify: exceptions, mutating variables, I/O, and so on.
Raising and Throwing
You’re likely familiar with raising Ruby exceptions ...
Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.