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

When using keyword arguments in mock expectations and calling with a hash the diff is confusing #1438

Open
JonRowe opened this issue Aug 12, 2021 · 1 comment

Comments

@JonRowe
Copy link
Member

JonRowe commented Aug 12, 2021

When you call keyword argument methods with hashes (e.g. if a method_missing doesn't have a proper ruby2_keywords call or a keyword splat) then the diff is confusing because it looks identical. This is going to be tricky to solve because to the differ, they are both hashes...

Steps to reproduce

class SomeClass
  def method_name(value:)
  end
end

RSpec.describe "SomeClass" do
  it "receives keyword arguments" do
    klass = SomeClass.new
    expect(klass).to receive(:method_name).with(value: [1,2,3])
    klass.method_name({value: [1, 2, 3]})
  end
end

Possible/suggested behaviour

 #<SomeClass (class)> received :method_name with unexpected arguments
         expected: (value: [1, 2, 3])
              got: ({:value =>[1, 2, 3]})

Actual behaviour

 #<SomeClass (class)> received :method_name with unexpected arguments
         expected: ({:value => [1, 2, 3]})
              got: ({:value => [1, 2, 3]})
@lukepearcewp
Copy link

I had a similar one to this today:

class Foo
  def bar(opts = {})
  end
end

expect(foo).to receive(:bar).with(baz: "should-have-used-a-hash")

#<Double "Foo"> received :bar with unexpected arguments
     expected: ({:baz=>"should-have-used-a-hash"})
          got: ({:baz=>"should-have-used-a-hash"})
     Diff:

Completely my mistake and it's correct but makes you think rspec is doing something funky at first glance. Would've been nice if the error could perhaps show *args vs **kwargs as the diff or something to help point you in the right direction.

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