Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have_attributes not showing a correct diff when used in a compound matcher #1448

Closed
simonc opened this issue Jan 30, 2024 · 2 comments
Closed

Comments

@simonc
Copy link

simonc commented Jan 30, 2024

Subject of the issue

When the have_attributes matcher is used as part of a compound matcher, its actual method is not used when building the diff message, resulting in a diff that is not showing the non-matching values. It seems that within a compound matcher the BaseMatcher#actual method is used.

Your environment

  • Ruby version: 2.7
  • rspec-expectations version: 3.12

Steps to reproduce

Call the have_attributes matcher within a compound matcher.

RSpec.describe 'A simple struct' do
  it 'has an id' do
    obj = Struct.new(:id).new(id: 1)
    expect(obj).to be_a(Struct).and have_attributes(id: 2)
  end
end

Expected behavior

The error message should be

Failure/Error: expect(obj).to be_a(Struct).and have_attributes(id: 2)

  expected #<struct id=1> to have attributes {:id => 2} but had attributes {:id => 1}
  Diff for (have attributes {:id => 2}):
  @@ -1 +1 @@
  -:id => 2,
  +:id => 1,

Actual behavior

The actual message is

Failure/Error: expect(obj).to be_a(Struct).and have_attributes(id: 2)

  expected #<struct id=1> to have attributes {:id => 2} but had attributes {:id => 1}
  Diff for (have attributes {:id => 2}):
  @@ -1 +1 @@
  -:id => 2,
  +"#<struct id=1>"
@pirj
Copy link
Member

pirj commented Feb 2, 2024

its actual method is not used when building the diff message

Can you please check what happens if you change this to it se the actual?

before you dive too deep, can you please check if #1440 fixes your case?

@simonc
Copy link
Author

simonc commented Feb 15, 2024

Hey @pirj! I upgraded to 3.13 and the issue does seem to be fixed. Thank you! 😊

@simonc simonc closed this as completed Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants