Skip to content

Commit 3b6a170

Browse files
committed
Use #public_send to disallow hitting private methods
1 parent b497851 commit 3b6a170

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/rspec/its.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ 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+
inner_subject.public_send(attr)
116116
end
117117
end
118118
end

spec/rspec/its_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,34 @@ def false_if_first_time
211211
expect(status).to eq(:passed)
212212
end
213213
end
214+
215+
describe "with private method" do
216+
subject do
217+
Class.new do
218+
def name
219+
private_name
220+
end
221+
private
222+
def private_name
223+
"John"
224+
end
225+
end.new
226+
end
227+
228+
context "when referring indirectly" do
229+
its(:name) { is_expected.to eq "John" }
230+
end
231+
232+
context "when attempting to refer directly" do
233+
context "it raises an error" do
234+
its(:private_name) do
235+
expect do
236+
should eq("John")
237+
end.to raise_error(NoMethodError)
238+
end
239+
end
240+
end
241+
end
214242
end
215243
end
216244
end

0 commit comments

Comments
 (0)