From 08d5c770c81403fc45e1e476e9df0bb5d807d100 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Tue, 1 Oct 2024 22:00:47 +0100 Subject: [PATCH] Support keyword arguments with and_invoke --- lib/rspec/mocks/message_expectation.rb | 1 + spec/rspec/mocks/and_invoke_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) 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..400b6edaa 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:| i ** 2 }, + lambda { |i:| 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