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

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.