collectiveidea / json_spec

Easily handle JSON in RSpec and Cucumber
rubygems.org/gems/json_spec
MIT License
919 stars 114 forks source link

be_json_eql with nested hashes succeeds when it should not #100

Closed gltarsa closed 7 years ago

gltarsa commented 7 years ago

In order to get better error messages and possibly improve my test cases I incorporated this gem into a complex test where we compare JSON returned from an API.

I am finding that tests with JSON containing nested hashes pass even when values in the internal hash are not the same. Here is a snippet of code I added to the be_json_eql_spec.rb file that demonstrates the problem. The first test passes, the second fails because the test is seeing a match where there is not one.

Am I missing something?

Thanks, Greg

  context "multi-level JSON" do
    let(:source_hash) do
      {
        data: { id: 401101 }
      }
    end

    it "matches multi-level JSON" do
      target_hash = JSON.parse(source_hash.to_json)
      expect(target_hash.to_json).to be_json_eql(source_hash.to_json)
    end

    it "does not match different multi-level JSON" do
      target_hash = JSON.parse(source_hash.to_json)
      target_hash['data']['id'] = 'not-the-same'
      expect(target_hash.to_json).not_to be_json_eql(source_hash.to_json)
    end
  end

The results look like this:

rspec
...................................................F.................................................................

Failures:

  1) JsonSpec::Matchers::BeJsonEql multi-level JSON does not match different multi-level JSON
     Failure/Error: expect(target_hash.to_json).not_to be_json_eql(source_hash.to_json)

       Expected inequivalent JSON
       Diff:

     # ./spec/json_spec/matchers/be_json_eql_spec.rb:28:in `block (3 levels) in <top (required)>'

Finished in 0.04953 seconds (files took 0.10741 seconds to load)
117 examples, 1 failure

Failed examples:

rspec ./spec/json_spec/matchers/be_json_eql_spec.rb:25 # JsonSpec::Matchers::BeJsonEql multi-level JSON does not match different multi-level JSON
akoltun commented 7 years ago

id key is excluded from comparison by default

https://github.com/collectiveidea/json_spec#exclusions

gltarsa commented 7 years ago

Duh. I had a big, complicated test where this was appearing to fail, but it turned out that I chose a nested 'id' field to try the gem out.

Sure enough, with a non-id field it works fine.

Sorry for the false alarm.