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 Mar 26, 2015
1 parent c51f4c4 commit f68e4c3
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 @@ -218,6 +218,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) unless RUBY_VERSION = '1.8.7'
end
end
end
end
end
context "with metadata" do
context "preserves access to metadata that doesn't end in hash" do
Expand Down

0 comments on commit f68e4c3

Please sign in to comment.