Composing Matchers

Even on their own, matchers are powerful tools for your tests. But they really shine when you compose them with other matchers to specify exactly what you expect (and nothing more). The result is more robust tests and fewer false failures.

There are a few different ways to compose matchers:

  • Pass one matcher directly into another
  • Embed matchers in Array and Hash data structures
  • Combine matchers with logical and/or operators

Before we consider these three cases, let’s look at how matchers determine whether or not the subject matches.

How Matchers Match Objects

Matchers build on top of one of Ruby’s standard protocols in order to provide composability: the humble === method. This method, often called “three-quals” or “case equality,” ...

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.