Let’s say that you want to test if a hash (hash_a) contains another hash (hash_b).
At the end of your test file, add the following matcher:
But hash_a and hash_b are not the greatest names for variables within your matcher.
In this case, hash_a is going to be the expected_hash and hash_b is going to be
the actual_hash
Using diffable
Now you have a working matcher, but is the output of your matcher good? Let’s make it fail:
It is easy to see why it’s failing because our hash is small, but if the hash was larger,
it would be difficult to see what is wrong with your test. So, you can use a built-in
function in Rspec: diffable.
The tests are looking pretty good now.
Using chain
Now let’s say that you have a hash of hashes and you want to know if that hash contains
a certain hash. You can use the built-in RSpec function chain: