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

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

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