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

`match` diff output should look like `match_array` when matching arrays

Open ghost opened this issue 2 years ago • 2 comments

Subject of the issue

expect(["a", "b", "c"]).to match(["a", "b"]) should print unexpected extra elements in a nice way like expect(["a", "b", "c"]).to match_array(["a", "b"])

Since match checks order while match_array does not, there is no way to get pretty diffs and check order of an array and get a nice diff. For very large matches (like API response matching) this makes the failure message hard to use. See this gist for an example

Your environment

  • Ruby version: 3.1.4
  • rspec-expectations version: 3.12.2

Steps to reproduce

RSpec.describe 'matching arrays' do
  it 'returns a nice diff when there are unexpected extra elements' do
    expect(["a", "b"]).to match(["a"])
  end
end

Expected behavior

expected collection contained:  ["a"]
actual collection contained:    ["a", "b"]
the extra elements were:        ["b"]

Actual behavior

expected ["a", "b", "c"] to match ["a", "b"]

ghost avatar Oct 03 '23 17:10 ghost