Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use #public_send to disallow hitting private methods #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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