O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required