Closed swrobel closed 1 year ago
The following crutch worked, but I have serious doubts that it would work correctly for all cases of user-provided blocks:
if block && block.parameters.first.first == :rest
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
end
So I ran your spec and it passed without any code changes on 3.1, so it only fails on 3.2 and using **kwargs
here in the block works correctly for both 3.1 and 3.2, so I'm honestly leaning towards saying "you should manage this block yourself here", I think any improvement we should make here should be more directed at it failing correctly...
Closing this but going to increase documentation over in #1525 this spec works as:
it "works for methods that accept keyword arguments" do
def instance.foo(bar: nil); bar; end
allow(instance).to receive(:foo).and_wrap_original { |m, **kwargs| m.call(**kwargs) }
expect(instance.foo(bar: "baz")).to eq("baz")
end
and_wrap_original doesn't work with kwargs in Ruby 3.2
Your environment
Steps to reproduce
Add the following spec to the "on a partial double" context of
and_wrap_original_spec.rb
, which is based on a similar spec fromand_call_original_spec.rb
:It will fail on the current version of rspec-mocks with
wrong number of arguments (given 1, expected 0)
. Work was done to fix this forand_call_original
in #1464 but a similar workaround was not added forand_wrap_original
. If you modify the definition ofand_wrap_original
like so:It will pass the additional spec above, but other specs will fail with:
I can't say that I understand enough about the inner workings of ruby2_keywords and its use with different types of procs/blocks to figure this out, otherwise I would've opened up a PR for the change. You can see my WiP on my branch here.
Expected behavior
and_wrap_original
works with methods that expect kwargsActual behavior
and_wrap_original
fails with methods that expect kwargs