Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 515
dostu Avatar asked Feb 18 '26 16:02

dostu


1 Answers

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:

  • https://github.com/rspec/rspec-support/issues/365
  • https://github.com/rspec/rspec-expectations/issues/1120

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

like image 64
Aleksis Zalitis Avatar answered Feb 21 '26 06:02

Aleksis Zalitis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!