Skip to content

Commit

Permalink
Use public_send
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Nov 4, 2024
1 parent b4a28c0 commit 8fabcc7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rspec/its/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def for(attribute, subject)
else
attribute_chain = attribute.to_s.split('.')
attribute_chain.inject(subject) do |inner_subject, attr|
inner_subject.send(attr)
inner_subject.public_send(attr)
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 @@ -393,4 +393,32 @@ def self.example(*_args)

its(:will_still_work) { is_expected.to be true }
end

context "with private method" do
subject(:klass) 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 { is_expected.to eq("John") }.to raise_error(NoMethodError)
end
end
end
end
end

0 comments on commit 8fabcc7

Please sign in to comment.