Skip to content

Commit

Permalink
Use #public_send to disallow hitting private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesottaway committed Dec 11, 2014
1 parent b497851 commit 5a89793
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rspec/its.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ def its(attribute, *options, &block)
else
attribute_chain = attribute.to_s.split('.')
attribute_chain.inject(subject) do |inner_subject, attr|
inner_subject.send(attr)
if inner_subject.respond_to?(:public_send)
inner_subject.public_send(attr)
else
inner_subject.send(attr)
end
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/rspec/its_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,34 @@ def false_if_first_time
expect(status).to eq(:passed)
end
end

describe "with private method" do
subject do
Class.new do
def name
private_name
end
private
def private_name
"John"
end
end.new
end

context "when referring indirectly" do
its(:name) { is_expected.to eq "John" }
end

context "when attempting to refer directly" do
context "it raises an error" do
its(:private_name) do
expect do
should eq("John")
end.to raise_error(NoMethodError)
end
end
end
end
end
end
end
Expand Down

0 comments on commit 5a89793

Please sign in to comment.