Search code examples
rubyrspecrspec-rails

Confusing RSpec hash match diff


I'm using RSpec match matcher to check if a hash contains expected values. When a key of the hash doesn't match, all the dynamic (a_string_starting_with, etc) values are shown as not matching. It's especially distracting when you try to match a bigger hash. I'm wondering if there's another way check the hash, so only the values which really do not match would show up in the diff.

Here's an example, where a is marked in red, although the value is correct.

it 'matches' do
  actual = {
    a: 'test test',
    b: 1,
    c: 2,
  }

  expect(actual).to match(
    a: a_string_starting_with('test'),
    b: 0,
    c: 2,
  )
end

Match diff

I'm wondering if there's another matcher I should use. Or if there are any custom matchers or gems for this?


Solution

  • The problem with this is the current differ gem used by RSpec and they are already aware of the issue, though currently no fix exists, as can be seen by these tickets:

    One of the solutions in proposed for now in the ticket is similar to what Mosaaleb is suggesting.