Jon Snow
Jon Snow

Reputation: 11892

Is there a way in RSpec to give 'not' higher precedence than the all matcher?

RSpec 3 provids the all matcher. For example,

expect(['Tom', 'Tony', 'Rosa']).to all( include("o") )

You can also have

expect(['Tom', 'Tony', 'Rosa']).to_not all( include("o") )

But how do you express "to all not"?

Upvotes: 1

Views: 114

Answers (2)

Dave Schweisguth
Dave Schweisguth

Reputation: 37647

Define a negated version of the matcher with which you want to test each element of the list, for example,

RSpec::Matchers.define_negated_matcher :exclude, :include

and use it like

expect(%w(Tom Tony Rosa)).to all(exclude('o'))

Upvotes: 3

Peter Alfvin
Peter Alfvin

Reputation: 29419

Per the documentation and a quick perusal of the code, there appears to be no built in all_not matcher, so one would have to iterate over the array to achieve the result, as follows:

['Tom', 'Tony', 'Rosa'].each { |name| expect(name).to_not contain('o') }

You can view the built-in matchers, including all at https://github.com/rspec/rspec-expectations/blob/9ff22694fa88cb0b0e794b6f99f3cfacfa909bf4/lib/rspec/matchers/built_in/all.rb

Upvotes: 0

Related Questions