Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Suggestion: Alternate Hash differ output #211

@hackling

Description

@hackling

I've have a custom matcher that I use when comparing hashes/ model objects.

The differ functionality is good, but when keys move around, especially when they aren't symbols and strings, it can sometimes be a little hard to get the information you are looking for. This becomes particularly useful when dealing with much large hashes or Rails model objects, as it only shows you the key value pairs that are incorrect, rather than all of them.

Here is an example of the current differ output and the output from my custom matcher. If there is interest in including it, I can share the rest of the code etc / potentially work on adding it as a feature.

example_spec.rb

require 'spec_helper'

describe 'diff output for tags' do
  let(:hash_1) do
    {
      :akey_1 => :value_1,
      :bkey_2 => :value_2,
      :bkey_3 => :value_3,
      :bkey_4 => :value_4,
    }
  end
  let(:hash_2) do
    {
      :bkey_2 => :value_2,
      :akey_1 => :value_3,
      :akey_4 => :value_5,
      :bkey_3 => :value_3,
    }
  end

  it 'outputs using the standard differ' do
    expect(hash_1).to eq hash_2
  end

  it 'outputs using the new differ' do
    expect(hash_1).to match_hash hash_2
  end
end

output.txt

  1) diff output for tags outputs using the standard differ
     Failure/Error: expect(hash_1).to eq hash_2

       expected: {:bkey_2=>:value_2, :akey_1=>:value_3, :akey_4=>:value_5, :bkey_3=>:value_3}
            got: {:akey_1=>:value_1, :bkey_2=>:value_2, :bkey_3=>:value_3, :bkey_4=>:value_4}

       (compared using ==)

       Diff:

       @@ -1,5 +1,5 @@
       -:akey_1 => :value_3,
       -:akey_4 => :value_5,
       +:akey_1 => :value_1,
        :bkey_2 => :value_2,
        :bkey_3 => :value_3,
       +:bkey_4 => :value_4,
     # ./spec/example_spec.rb:22:in `block (2 levels) in <top (required)>'

  2) diff output for tags outputs using the new differ
     Failure/Error: expect(hash_1).to match_hash hash_2
       key: :akey_1 expected: :value_3 got: :value_1
       key: :akey_4 expected: :value_5 got: <missing>
       key: :bkey_4 expected: <missing> got: :value_4
     # ./spec/example_spec.rb:26:in `block (2 levels) in <top (required)>'

Finished in 0.75406 seconds (files took 0.01402 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/example_spec.rb:21 # diff output for tags outputs using the standard differ
rspec ./spec/example_spec.rb:25 # diff output for tags outputs using the new differ

Rerun all failed examples:

rspec ./spec/example_spec.rb:{21,25}

An example with a much larger hash

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions