Reputation: 2920
In the result of my VCR cassette I've got an array of hashes:
[{
'key' => 'TP-123',
'status:' => 'test'
},
{
'key' => 'TP-124',
'status:' => 'test'
},
{
'key' => 'TP-125',
'status:' => 'test'
},
{
'key' => 'TP-126',
'status:' => 'test'
},
]
I want to check if there is a hash with 'key' => 'TPFJT-41'
expect(fetcher).not_to include('key' => 'TPFJT-41')
it seems to not iterate through the whole array of hashes but it takes the first value - when I change it to:
expect(fetcher).not_to include('key' => 'TP-126')
it will pass either
Upvotes: 1
Views: 155
Reputation: 3029
it seems to not iterate through the whole array of hashes but it takes the first value
No. It doesn't take the first value only. And it does iterate. The issue is that you expect include
matcher to do something that it does not do in fact. It is not smart enough to look into the nested objects :) When applied to an array it just checks that the object exists in the array.
So, in your case, it checks whether a hash {'key' => 'TPFJT-41'}
exists or not. It doesn't, obviously, so your negated expectation is always green (but the spec is broken).
One of the ways to fix it is to transform the result before checking it. For example, something like the following should work:
expect(fetcher.map { |h| h['key'] }).not_to include('TPFJT-41')
Upvotes: 4