I need to verify the contents of a hash, and I was surprised to find that RSpec's contain_exactly
only works on arrays. The ideal expectation is:
expect(type.values.values).to contain_exactly(
ONE: an_object_having_attributes(value: 'uno'),
TWO: an_object_having_attributes(value: 'dos')
)
The base requirement is that contain_exactly
requires that the array have only those elements, and a hash equivalent must only contain the exact key/value pairs specified.
There's plenty of workarounds that are just okay:
include(key: value)
, but this allows other keys, I need an exact match.expect(hash.keys).to contain_exactly(...)
but that doesn't verify that the keys are specifically linked to the values.- use
contain_exactly
anyway (which reads the hash as a tuple of[key, value]
) and match based on the sub-array e.g.contain_exactly(a_collection_containing_exactly('ONE', an_object_having_attributes(value: 'uno')), ...)
- iterate through the hash and match keys to values based on an expected input with
aggregate_failures
.
etc. but I'm mostly curious if there's a built-in RSpec way to do this.