Skip to content

Commit 3d801ae

Browse files
committed
Use #public_send to disallow hitting private methods
1 parent c51f4c4 commit 3d801ae

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/rspec/its.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ def its(attribute, *options, &block)
112112
else
113113
attribute_chain = attribute.to_s.split('.')
114114
attribute_chain.inject(subject) do |inner_subject, attr|
115-
inner_subject.send(attr)
115+
if inner_subject.respond_to?(:public_send)
116+
inner_subject.public_send(attr)
117+
else
118+
inner_subject.send(attr)
119+
end
116120
end
117121
end
118122
end

spec/rspec/its_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,34 @@ def false_if_first_time
218218
expect(status).to eq(:passed)
219219
end
220220
end
221+
222+
describe "with private method" do
223+
subject do
224+
Class.new do
225+
def name
226+
private_name
227+
end
228+
private
229+
def private_name
230+
"John"
231+
end
232+
end.new
233+
end
234+
235+
context "when referring indirectly" do
236+
its(:name) { is_expected.to eq "John" }
237+
end
238+
239+
context "when attempting to refer directly" do
240+
context "it raises an error" do
241+
its(:private_name) do
242+
expect do
243+
should eq("John")
244+
end.to raise_error(NoMethodError)
245+
end
246+
end
247+
end
248+
end
221249
end
222250
context "with metadata" do
223251
context "preserves access to metadata that doesn't end in hash" do

0 commit comments

Comments
 (0)