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

Commit

Permalink
Do not change method visibility when stubbing private class methods. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kcboschert authored and JonRowe committed May 29, 2019
1 parent 5f3f68f commit b3f1868
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Enhancements:
`expect(Class).to receive(:method).exactly(1).time`.
(Pistos, Benoit Tigeot, #1271)

Bug Fixes:

* Do not change the visibility of stubbed private class methods. (Kevin Boschert)

### 3.8.0 / 2018-08-04
[Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.7.0...v3.8.0)

Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/mocks/method_double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def define_proxy_method
return if @method_is_proxied

save_original_implementation_callable!
definition_target.class_exec(self, method_name, visibility) do |method_double, method_name, visibility|
definition_target.class_exec(self, method_name, @original_visibility || visibility) do |method_double, method_name, visibility|
define_method(method_name) do |*args, &block|
method_double.proxy_method_invoked(self, *args, &block)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rspec/mocks/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def existing_instance_method
existing_private_instance_method
end

private
def existing_private_instance_method
:original_value
end
Expand Down Expand Up @@ -84,6 +85,16 @@ def existing_private_instance_method
expect { @stub.dup.foobar }.to raise_error NoMethodError, /foobar/
end

it "remains private when it stubs a private instance method" do
allow(@instance).to receive(:existing_private_instance_method).and_return(1)
expect { @instance.existing_private_instance_method }.to raise_error NoMethodError, /private method `existing_private_instance_method/
end

it "remains private when it stubs a private class method" do
allow(@class).to receive(:existing_private_class_method).and_return(1)
expect { @class.existing_private_class_method }.to raise_error NoMethodError, /private method `existing_private_class_method/
end

context "using `with`" do
it 'determines which value is returned' do
allow(@stub).to receive(:foo).with(1) { :one }
Expand Down

0 comments on commit b3f1868

Please sign in to comment.