leifg
leifg

Reputation: 9028

RSpec custom partial hash matching

I am currently writing test suite in RSpec.

I read a lot of the documentation and know how to do partial hash mapping.

expect(result).to include({key1: 'value1', key2: 'value2'})

Or if you just want to check for keys:

expect(result).to include(:key1, :key2)

However I want to do something more fuzzy. I want to check for the type of the value. Something like:

expect(result).to include({key1: instance_of(String), key2: instance_of(String)})

But RSpec doesn't like that. It will always try to compare 'valuex' to the RSpec ArgumentMatcher (which of course failes).

Is there a way to do this without custom matchers?

Upvotes: 2

Views: 1512

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176562

Test the specific keys with separate assertions

expect(result[:key1]).to be_a(String)
expect(result[:key2]).to be_a(String)

Upvotes: 3

Related Questions