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

Argument matcher prevents diffability of custom matcher #1480

Open
jasonkarns opened this issue Sep 3, 2022 · 1 comment
Open

Argument matcher prevents diffability of custom matcher #1480

jasonkarns opened this issue Sep 3, 2022 · 1 comment

Comments

@jasonkarns
Copy link

Subject of the issue

We have a custom matcher that matches "as_json-able" objects.

RSpec::Matchers.define :as_json do |expected|
  def actual = super.as_json
  match { |actual| values_match? expected, actual }
  diffable
end

For diffing, it redefines the actual.

This matcher correctly emits diffs when used "directly" but when used as an argument matcher within a mock, the diff output no longer respects the redefined actual.

I suspect this may be related to rspec/rspec-expectations#1317

Your environment

  • Ruby version: 3.1.0p0
  • rspec-mocks version: 3.11.1
    rspec (3.11.0)
    rspec-core (3.11.0)
    rspec-expectations (3.11.0)
    rspec-mocks (3.11.1)
    rspec-rails (5.1.2)
    rspec-support (3.11.0)
    diff-lcs (1.5.0)

Steps to reproduce

https://gist.github.com/jasonkarns/7ac8c418dc155ef3d3555c3a0d9ecc64

Expected behavior

Given the tests in linked gist above, I would expect the failure output to print the diffable values comparing the overridden actual:

something like:

       expected #<JsonAble:0x00000001102b23f0 @a=42> to as json {:oops=>42}
       Diff:
       @@ -1 +1 @@
       -:oops => 42,
       +:alt => 42,

Actual behavior

The argument matcher failure message ignores the overridden actual value of the nested matcher and instead diffs against the original actual:

       #<Double (anonymous)> received :serialize with unexpected arguments
         expected: (as json {:oops=>42})
              got: (#<JsonAble:0x00000001101691b0 @a=42>)
       Diff:
       @@ -1 +1 @@
       -["as json {:oops=>42}"]
       +[#<JsonAble:0x00000001101691b0 @a=42>]
@pirj
Copy link
Member

pirj commented Sep 3, 2022

Wondering if rspec/rspec-expectations#1319 fixes this?

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