Skip to content

Hash include with composable matcher gives misleading diff #86

@owst

Description

@owst

Expecting a hash to include another when using composable matchers gives a misleading diff for the subset of the hash that did match:

  1) includes should include {:a => /2/, :b => "bar"}
     Failure/Error:
       expect(
         {
           a: '123',
           b: 'foo',
         }
       ).to include(a: /2/, b: 'bar')
     
       expected {:a => "123", :b => "foo"} to include {:b => "bar"}
       Diff:
       @@ -1,3 +1,3 @@
       -:a => /2/,
       -:b => "bar",
       +:a => "123",
       +:b => "foo",

Notice that

-:a => /2/,
+:a => "123",

is in the diff, despite "123" being matched by /2/ - this can make it harder to determine what the problem is, with a larger hash.

N.b. that the expected {:a => "123", :b => "foo"} to include {:b => "bar"} part of the failure message is good, and does indicate the problem - this issue is only about the "Diff:" part.

Your environment

Resolving dependencies...
Using bundler 1.17.3
Using diff-lcs 1.3
Using rspec-support 3.7.1
Using rspec-core 3.7.1
Using rspec-expectations 3.7.0
Using rspec-mocks 3.7.0
Using rspec 3.7.0
Ruby version is: 2.5.3

Steps to reproduce

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  gem "rspec", "3.7.0" # Activate the gem and version you are reporting the issue against.
end

puts "Ruby version is: #{RUBY_VERSION}"
require 'rspec/autorun'

RSpec.describe 'includes' do
  it do
    expect(
      {
        a: '123',
        b: 'foo',
      }
    ).to include(a: /2/, b: 'bar')
  end
end

Expected behavior

The diff does not highlight matched entries as different:

@@ -1,3 +1,3 @@
 :a => "123",
-:b => "bar",
+:b => "foo",

Actual behavior

The diff does highlight matched entries as different:

@@ -1,3 +1,3 @@
-:a => /2/,
-:b => "bar",
+:a => "123",
+:b => "foo",

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