Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Fix and_call_original for Ruby 3.2
Browse files Browse the repository at this point in the history
When the original method isn't present, a proc that sends
method_missing is returned. This block must support ruby2_keywords
in order to work correctly with Ruby 3.2
  • Loading branch information
igor-drozdov authored and Igor Drozdov committed Jul 5, 2023
1 parent 09d2760 commit 623ee32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
15 changes: 11 additions & 4 deletions lib/rspec/mocks/method_double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ def original_implementation_callable
# handler of the object. This accounts for cases where the user has not
# correctly defined `respond_to?`, and also 1.8 which does not provide
# method handles for missing methods even if `respond_to?` is correct.
@original_implementation_callable ||= original_method ||
Proc.new do |*args, &block|
@object.__send__(:method_missing, @method_name, *args, &block)
end
@original_implementation_callable ||= original_method || method_missing_block
end

alias_method :save_original_implementation_callable!, :original_implementation_callable
Expand All @@ -40,6 +37,16 @@ def original_method
@proxy.original_method_handle_for(method_name)
end

# @private
def method_missing_block
block = Proc.new do |*args, &block|
@object.__send__(:method_missing, @method_name, *args, &block)
end
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)

block
end

# @private
def visibility
@proxy.visibility_for(@method_name)
Expand Down
23 changes: 17 additions & 6 deletions spec/rspec/mocks/and_call_original_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,15 @@ def instance.foo(bar: nil); bar; end
klass.class_exec do
private

def method_missing(name, *args)
def existing_method_with_args(arg, kwarg:)
[arg, kwarg]
end

def method_missing(name, *args, **kwargs)
if name.to_s == "greet_jack"
"Hello, jack"
["Hello, jack", *args, kwargs]
elsif name.to_s == "existing_method"
existing_method_with_args(*args, **kwargs)
else
super
end
Expand All @@ -259,13 +265,18 @@ def method_missing(name, *args)
end

it 'works when the method_missing definition handles the message' do
expect(instance).to receive(:greet_jack).and_call_original
expect(instance.greet_jack).to eq("Hello, jack")
expect(instance).to receive(:greet_jack).with(:arg, kwarg: 1).and_call_original
expect(instance.greet_jack(:arg, kwarg: 1)).to eq(["Hello, jack", :arg, { kwarg: 1 }])
end

it 'works for an any_instance partial mock' do
expect_any_instance_of(klass).to receive(:greet_jack).and_call_original
expect(instance.greet_jack).to eq("Hello, jack")
expect_any_instance_of(klass).to receive(:greet_jack).with(:arg, kwarg: 1).and_call_original
expect(instance.greet_jack(:arg, kwarg: 1)).to eq(["Hello, jack", :arg, { kwarg: 1 }])
end

it 'works for the method propagated by method missing' do
expect(instance).to receive(:existing_method).with(:arg, kwarg: 1).and_call_original
expect(instance.existing_method(:arg, kwarg: 1)).to eq([:arg, 1])
end

it 'raises an error for an unhandled message for an any_instance partial mock' do
Expand Down

0 comments on commit 623ee32

Please sign in to comment.