Defining Matcher Aliases

In Passing One Matcher Into Another, you saw how RSpec defines a_value_within as an alias of the be_within matcher. It allows you to write expectations that read smoothly like the following one:

 expect​(results).to start_with a_value_within(0.1).of(Math::PI)

You can use the same techniques in your own projects. Just call alias_matcher with the name of the new matcher first, followed by the existing one (the same order you’d use with Ruby’s alias_method):

 RSpec::Matchers.alias_matcher ​:an_admin​, ​:be_an_admin

This snippet defines a new method, an_admin, which wraps the existing be_an_admin matcher (a dynamic predicate ...

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.