-
-
Notifications
You must be signed in to change notification settings - Fork 358
Issue with kwargs in and_invoke
#1594
Comments
It is unclear to me art a glance, what is this example covering? Is it making an expectation that a method is called on an instance? Or does it replace the methid’s implementation and checks how its consumer work? I see that in theory a combination of allow+receive+and_invoke and a subsequent expect+receive+with intuitively should work, but how often is this needed in practice? |
Thank you for the prompt reply. For example, the mocked method might sit somewhere deep inside our code, and our code would depend on what it returns. And in turn, what the mock will return will depend on what we pass to it from our code: # somewhere deep inside our code
object_with_mocked_method = ThirdPartyLibrary.new
# NB: The return value from the mock will depend on what we pass to it from our code.
mocked_method_return_value = object_with_mocked_method.mocked_method(argument_passed_from_our_code:)
case mocked_method_return_value
when 'this_result' then do_this
when 'that_result' then do_that
else do_something_different
end UPD: Of course, we can create a series of allow(object_with_mocked_method).to receive(:mocked_method).with('this_argument').and_return('this_result')
allow(object_with_mocked_method).to receive(:mocked_method).with('that_argument').and_return('that_result') …but it would be not as convenient as this: let(:responses) do
{
'this_argument' => 'this_result',
'that_argument' => 'that_result',
# etc.
}
end
before do
allow(object_with_mocked_method).to receive(:mocked_method).and_invoke(
->(argument_passed_from_our_code:) { responses[argument_passed_from_our_code] }
)
end |
Can you give #1595 a spin to see if that fixes your issue? I think its just a spot that hasn't been marked as passing through keyword arguments properly, a lot of our code predates the split of keyword arguments out of *args |
@JonRowe I updated my source 'https://rubygems.org'
ruby '3.3.4'
gem 'rspec-core', github: 'rspec/rspec-core', branch: 'main'
gem 'rspec-expectations', github: 'rspec/rspec-expectations', branch: 'main'
gem 'rspec-support', github: 'rspec/rspec-support', branch: 'main'
gem 'rspec-mocks', github: 'rspec/rspec-mocks', branch: 'support-kw-args-with-and-invoke' …and ran the specs from the example I provided, it passes! Thank you and the rest of the team for all the hard work! |
Released in 3.13.2 |
Subject of the issue
When lambdas passed to
and_invoke
are called with keyword arguments,rspec-mocks
fails withArgumentError: wrong number of arguments
.Changing
RSpec::Mocks::AndInvokeImplementation#call
to……seems to fix the issue.
Your environment
Steps to reproduce
Expected behavior
Actual behavior
The text was updated successfully, but these errors were encountered: