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

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

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