How Matchers Work

Earlier, we saw that matchers are objects that carry pass/fail logic. Let’s take a closer look at how they work.

A matcher is a bit like a regular expression. Just as the regular expression /^\[warn\] / defines a category of strings (those beginning with [warn] followed by a space), a matcher defines a category of objects.

The resemblance doesn’t end there. As you’ll see in a moment, matchers implement one of the same protocols that regular expressions do, allowing them to be used in similar ways.

Any Ruby object can be used as a matcher as long as it implements a minimal set of methods. This protocol is so simple that you can even build up a matcher object in IRB. Over the next few code examples, we’re going to do just that. ...

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.