Zack Xu Zack Xu - 3 months ago 9
Ruby Question

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"?

Answer

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'))