Generated Example Descriptions

Matchers have another useful ability over simpler assert methods: they’re self-describing. The matcher protocol includes the optional (but recommended) description method. All built-in matchers define this method:

 >>​ start_with(1).description
 => "start with 1"
 >>​ (start_with(1) & end_with(9)).description
 => "start with 1 and end with 9"
 >>​ contain_exactly( a_string_starting_with(1) & ending_with(9) ).description
 => "contain exactly (a string starting with 1 and ending with 9)"

As you can see, the descriptions of composed and compound matchers include the description of each part. These descriptions are used in failure messages when you pass one matcher to another. They can also help you reduce duplication ...

Get Effective Testing with RSpec 3 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.