From 2785b7e31a92c6521808c421ada6374d9a9b324b Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Wed, 2 Oct 2024 14:16:03 +0100 Subject: [PATCH] Merge pull request #1595 from rspec/support-kw-args-with-and-invoke Support keyword arguments with and_invoke --- Changelog.md | 4 ++++ lib/rspec/mocks/message_expectation.rb | 1 + spec/rspec/mocks/and_invoke_spec.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/Changelog.md b/Changelog.md index ef42586f5..bffa20aa8 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,10 @@ ### Development [Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.13.1...3-13-maintennace) +Bug Fixes: + +* Support keyword arguments in callables passed to `and_invoke`. (Jon Rowe, #1595) + ### 3.13.1 / 2024-05-08 [Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.13.0...v3.13.1) diff --git a/lib/rspec/mocks/message_expectation.rb b/lib/rspec/mocks/message_expectation.rb index 8376a1c7a..1ecfa3e8a 100644 --- a/lib/rspec/mocks/message_expectation.rb +++ b/lib/rspec/mocks/message_expectation.rb @@ -748,6 +748,7 @@ def call(*args, &block) proc.call(*args, &block) end + ruby2_keywords(:call) if respond_to?(:ruby2_keywords, true) end # Represents a configured implementation. Takes into account diff --git a/spec/rspec/mocks/and_invoke_spec.rb b/spec/rspec/mocks/and_invoke_spec.rb index cafa16d3e..5a4f2fd3a 100644 --- a/spec/rspec/mocks/and_invoke_spec.rb +++ b/spec/rspec/mocks/and_invoke_spec.rb @@ -39,6 +39,18 @@ module Mocks expect(dbl.square_then_cube(2)).to eq 4 expect(dbl.square_then_cube(2)).to eq 8 end + + if RSpec::Support::RubyFeatures.kw_args_supported? + binding.eval(<<-RUBY, __FILE__, __LINE__) + it 'passes keyword arguments into the callable' do + expect(dbl).to receive(:square_then_cube).and_invoke(lambda { |i: 1| i ** 2 }, + lambda { |i: 1| i ** 3 }) + + expect(dbl.square_then_cube(i: 2)).to eq 4 + expect(dbl.square_then_cube(i: 2)).to eq 8 + end + RUBY + end end end end