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

Negating Matchers

In Putting the Pieces Together, we saw RSpec’s not_to and to_not methods, which specify that a given condition should not hold:

 expect​(correct_grammar).to_not split_infinitives

If you find yourself doing this over and over again, you can define a negated matcher that you’d use like so:

 expect​(correct_grammar).to avoid_splitting_infinitives

It’s easy to create your own negated matcher. All you have to do is call define_negated_matcher:

 RSpec::Matchers.define_negated_matcher ​:avoid_splitting_infinitives​,
 :split_infinitives

As with alias_matcher, you pass the name of ...

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