Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9eb8906

Browse files
committedOct 2, 2024·
Mark any_instance proxy methods as private if they were private previously
1 parent d8f0054 commit 9eb8906

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed
 

‎lib/rspec/mocks/any_instance/recorder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,12 @@ def observe!(method_name)
259259
@observed_methods << method_name
260260
backup_method!(method_name)
261261
recorder = self
262+
method_was_private = @klass.private_method_defined?(method_name)
262263
@klass.__send__(:define_method, method_name) do |*args, &blk|
263264
recorder.playback!(self, method_name)
264265
__send__(method_name, *args, &blk)
265266
end
267+
@klass.__send__(:private, method_name) if method_was_private
266268
@klass.__send__(:ruby2_keywords, method_name) if @klass.respond_to?(:ruby2_keywords, true)
267269
end
268270

‎spec/rspec/mocks/any_instance_spec.rb

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,22 +1074,42 @@ def foo; end
10741074
end
10751075

10761076
context "private methods" do
1077-
before :each do
1078-
allow_any_instance_of(klass).to receive(:private_method).and_return(:something)
1077+
before(:example) { allow_any_instance_of(klass).to receive(:private_method).and_return(:something) }
10791078

1080-
verify_all
1079+
let(:object) { klass.new }
1080+
1081+
it "maintains the method in the list of private_methods" do
1082+
expect {
1083+
verify_all
1084+
}.to_not change { object.private_methods.include?(:private_method) }.from(true)
10811085
end
10821086

1083-
it "cleans up the backed up method" do
1084-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_falsey
1087+
it "maintains the methods exclusion from the list of public_methods" do
1088+
expect {
1089+
verify_all
1090+
}.to_not change { object.public_methods.include?(:private_method) }.from(false)
10851091
end
10861092

1087-
it "restores a stubbed private method after the spec is run" do
1093+
it "maintains the methods visibility" do
1094+
expect { klass.new.private_method }.to raise_error(NoMethodError)
1095+
expect(klass.new.send(:private_method)).to eq(:something)
10881096
expect(klass.private_method_defined?(:private_method)).to be_truthy
10891097
end
10901098

1091-
it "ensures that the restored method behaves as it originally did" do
1092-
expect(klass.new.send(:private_method)).to eq(:private_method_return_value)
1099+
context "after the spec has run" do
1100+
before(:example) { verify_all }
1101+
1102+
it "cleans up the backed up method" do
1103+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_falsey
1104+
end
1105+
1106+
it "restores a stubbed private method after the spec is run" do
1107+
expect(klass.private_method_defined?(:private_method)).to be_truthy
1108+
end
1109+
1110+
it "ensures that the restored method behaves as it originally did" do
1111+
expect(klass.new.send(:private_method)).to eq(:private_method_return_value)
1112+
end
10931113
end
10941114
end
10951115
end
@@ -1098,7 +1118,7 @@ def foo; end
10981118
context "private methods" do
10991119
before :each do
11001120
expect_any_instance_of(klass).to receive(:private_method).and_return(:something)
1101-
klass.new.private_method
1121+
klass.new.send(:private_method)
11021122

11031123
verify_all
11041124
end
@@ -1109,6 +1129,7 @@ def foo; end
11091129

11101130
it "restores a stubbed private method after the spec is run" do
11111131
expect(klass.private_method_defined?(:private_method)).to be_truthy
1132+
expect(klass.new.private_methods).to include :private_method
11121133
end
11131134

11141135
it "ensures that the restored method behaves as it originally did" do

0 commit comments

Comments
 (0)
This repository has been archived.