Higher-Order Matchers

All the matchers seen so far are primitives. Now, we’re going to look at higher-order matchers—that is, matchers that you can pass other matchers into. With this technique, you can build up composed matchers that specify exactly the behavior you want.

Collections and Strings

One of the primary tasks of programming, in any language, is dealing with collections, and Ruby is no exception. RSpec ships with six different matchers for dealing with data structures:

  • include requires certain items to be present (in any order).
  • start_with and end_with require items to be at the beginning or end.
  • all checks a common property across all items.
  • match checks a data structure against a pattern.
  • contain_exactly requires certain items, and ...

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

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