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

Block matchers observe a block of code and are used to specify a side effect that occurs when the block runs. They take the form:

 expect​ { some_code }.to matcher

As with value matchers, block matchers can be negated by using not_to or to_not rather than to.

Mutation

The change matcher captures a value before running the block (old_value) and again after running the block (new_value). The value can be specified in two ways:

 expect​ { do_something }.to change(obj, ​:attr​)
 # or
 expect​ { do_something }.to change { obj.attr }

It supports a rich fluent interface for specifying further details about the mutation:

MatcherPasses if…

change { }

old_value != new_value

change { }.by(x)

(new_value - old_value) == x

change { }.by_at_least(x) ...

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