-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Proposal: Inform user when testing a private method #82
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ Feature: attribute of subject | |
Person | ||
with one phone number (555-1212) | ||
phone_numbers.first | ||
is expected to eq "555-1212" | ||
should eq "555-1212" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||
""" | ||
|
||
Scenario: specify value of an attribute of a hash | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,7 +95,7 @@ module Its | |
# | ||
# # This ... | ||
# describe Array do | ||
# its(:size, :focus) { should eq(0) } | ||
# its(:size, focus: true) { should eq(0) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverted it again :) |
||
# end | ||
# | ||
# # ... generates the same runtime structure as this: | ||
|
@@ -131,7 +131,31 @@ def its(attribute, *options, &block) | |
else | ||
attribute_chain = attribute.to_s.split('.') | ||
attribute_chain.inject(subject) do |inner_subject, attr| | ||
|
||
# When NilClass: user didnt set the rspec setting | ||
# When FalseClass: skip this block | ||
if RSpec.configuration.its_private_method_debug.is_a? NilClass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# respond_to? only looks in the public method space | ||
# TODO Check NoMethodError | ||
# TODO Check if inner_subject some kind of const | ||
# inner_subject.method_defined?(attr) && !inner_subject.respond_to?(attr) | ||
if !inner_subject.respond_to?(attr) | ||
|
||
if RSpec.configuration.its_private_method_debug | ||
# TODO if debug: show spec:line_num for better indication | ||
puts its_caller.first | ||
end | ||
|
||
puts "You are testing a private method. Set RSpec its_private_method_debug setting to show more info or hide it completly." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
|
||
# TODO Add deprecation warning to drop private method calling | ||
# for the next major release | ||
end | ||
end | ||
|
||
inner_subject.send(attr) | ||
|
||
end | ||
end | ||
end | ||
|
@@ -175,6 +199,11 @@ def should_not(matcher=nil, message=nil) | |
end | ||
|
||
RSpec.configure do |rspec| | ||
|
||
# Add RSpec configuration setting to allow | ||
# the user to handle private method invoking warning | ||
rspec.add_setting :its_private_method_debug | ||
|
||
rspec.extend RSpec::Its | ||
rspec.backtrace_exclusion_patterns << %r(/lib/rspec/its) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,19 @@ module RSpec | |
its([]) { expect(described_class).to be Its } | ||
end | ||
end | ||
context "" do | ||
subject do | ||
Class.new do | ||
private | ||
|
||
def name | ||
'Maria' | ||
end | ||
end.new | ||
end | ||
|
||
its(:name, :focus) { should eq('Maria') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JFYI |
||
end | ||
context "with explicit subject" do | ||
subject do | ||
Class.new do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gemfile-custom
is the best place for it.