Skip to content

Commit

Permalink
Merge pull request #1552 from igor-drozdov/ruby2-keywords-for-missing…
Browse files Browse the repository at this point in the history
…-method-double

Fix and_call_original for Ruby 3.2
  • Loading branch information
JonRowe committed Jul 6, 2023
2 parents 09d2760 + c926345 commit 8fa2c12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 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, &b|
@object.__send__(:method_missing, @method_name, *args, &b)
end
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)

block
end

# @private
def visibility
@proxy.visibility_for(@method_name)
Expand Down
34 changes: 34 additions & 0 deletions spec/rspec/mocks/and_call_original_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,40 @@ def instance.foo(bar: nil); bar; end
CODE
end

if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-RUBY, __FILE__, __LINE__)
context 'on an object with a method propagated by method_missing' do
before do
klass.class_exec do
private
def call_method_with_kwarg(arg, kwarg:)
[arg, kwarg]
end
def method_missing(name, *args, **kwargs)
if name.to_s == "method_with_kwarg"
call_method_with_kwarg(*args, **kwargs)
else
super
end
end
end
end
it 'works for the method propagated by method missing' do
expect(instance).to receive(:method_with_kwarg).with(:arg, kwarg: 1).and_call_original
expect(instance.method_with_kwarg(:arg, kwarg: 1)).to eq([:arg, 1])
end
it 'works for the method of any_instance mock propagated by method missing' do
expect_any_instance_of(klass).to receive(:method_with_kwarg).with(:arg, kwarg: 1).and_call_original
expect(instance.method_with_kwarg(:arg, kwarg: 1)).to eq([:arg, 1])
end
end
RUBY
end

context 'on an object that defines method_missing' do
before do
klass.class_exec do
Expand Down

0 comments on commit 8fa2c12

Please sign in to comment.