rspec-support icon indicating copy to clipboard operation
rspec-support copied to clipboard

Bug: Issue with Differ due to new ObjectFormatter class

Open hackling opened this issue 10 years ago • 2 comments

If you use an Rspec matcher inside a hash, and there is something wrong with the hash, even if the matcher was correct, the ObjectInspector inspects the matcher before passing it to the differ, and then as a result, tells you that they didn't match.

require 'spec_helper'

describe 'issue with the new ObjectInspector' do
  let(:expectation) do
    {
      decimal: 0.000005,
      string: 'word',
    }
  end

  it 'outputs correctly' do
    expect(expectation).to match(
      decimal: be_within(0.1).of(0.000004),
      string: 'word',
    )
  end

  it 'outputs weirdly' do
    expect(expectation).to match(
      decimal: be_within(0.1).of(0.000004),
      string: 'wordx',
    )
  end
end
# Run options: exclude {:skip=>true}
#
#   1) issue with the new ObjectInspector outputs weirdly
#      Failure/Error: expect(expectation).to match(
#        expected {:decimal=>5.0e-06, :string=>"word"} to match {:decimal=>(be within 0.1 of 4.0e-06), :string=>"wordx"}
#        Diff:
#        @@ -1,3 +1,3 @@
#        -:decimal => (be within 0.1 of 4.0e-06),
#        -:string => "wordx",
#        +:decimal => 5.0e-06,
#        +:string => "word",
#
#      # ./example-breaking.rb:19:in `block (2 levels) in <top (required)>'
#
#  2/2 |============================================================ 100 ============================================================>| Time: 00:00:00
#
# Finished in 0.60996 seconds (files took 6.8 seconds to load)
#2 examples, 1 failure
#
# Failed examples:
#
# rspec ./example-breaking.rb:18 # issue with the new ObjectInspector outputs weirdly

hackling avatar Jul 10 '15 05:07 hackling

Yep, this is a known issue. There's some prior discussion about it in rspec/rspec#79. Given that we're doing a simple textual diff, I'm not sure what the solution is. Any ideas?

myronmarston avatar Jul 10 '15 05:07 myronmarston

It would have to involve something like evaluating the composed matcher, and then if it evaluates to true, maybe override the content of the hashes so that it doesn't trigger the differ.

hackling avatar Jul 10 '15 06:07 hackling

Closing due to inactivity as part of the monorepo migration, there are several similar issues about improving the differ and we intend to make this pluggable.

JonRowe avatar Nov 28 '24 18:11 JonRowe