I have an Array/Enumerable where each entity is a Hash. Using RSpec what is the most idiomatic way to test for “none of the entries in the enumerable should have a key called 'body'“?
I can do something like:
array.none? {|thing| thing.key? 'body'}.should be_true
or
array.should be_none {|thing| thing.key? 'body'}
…but there must be a more RSpec-way of doing this, correct?
I can’t seem to find an appropriate built-in matcher. Is the answer a custom matcher?
I would use
responses.should be_none { |response| response.key? 'body' }Between the two you gave. This would be slightly more helpful with an error like
“Expected none? to return true”
where as your first example would say something like
“expected False:false class to be true”
The third option I can see would be something like
This would give an error like
expected [“foo”, “bar”, “body”] not to include “body”
Other than that, looking at https://www.relishapp.com/rspec/rspec-expectations/v/2-11/docs/built-in-matchers/satisfy-matcher
you could try
responses.should satisfy { |response| not response.keys.include? "body" }