-
-
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
Open
zirni
wants to merge
3
commits into
rspec:main
Choose a base branch
from
zirni:feature/safely-indicate-calling-private-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
require 'rspec/its/version' | ||
require 'rspec/core' | ||
|
||
RSpec.deprecate <<EOS | ||
rspec-its deprecates calling _send_ in favour of _public_send_ internally. | ||
This change will be introduced with version 2.0. | ||
Maybe you're testing private methods in your test suite without your intention. | ||
If you need more information about which test is affected, | ||
set `config.its_raise_errors_for_private_method_calling = true` | ||
EOS | ||
|
||
module RSpec | ||
module Its | ||
|
||
|
@@ -95,7 +103,7 @@ module Its | |
# | ||
# # This ... | ||
# describe Array do | ||
# its(:size, focus: true) { should eq(0) } | ||
# its(:size, :focus) { should eq(0) } | ||
# end | ||
# | ||
# # ... generates the same runtime structure as this: | ||
|
@@ -132,30 +140,18 @@ def its(attribute, *options, &block) | |
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 | ||
if RSpec.configuration.its_raise_errors_for_private_method_calling | ||
|
||
# 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." | ||
|
||
# TODO Add deprecation warning to drop private method calling | ||
# for the next major release | ||
raise 'Testing private method' + attr | ||
end | ||
end | ||
|
||
inner_subject.send(attr) | ||
|
||
end | ||
end | ||
end | ||
|
@@ -202,7 +198,7 @@ def should_not(matcher=nil, message=nil) | |
|
||
# Add RSpec configuration setting to allow | ||
# the user to handle private method invoking warning | ||
rspec.add_setting :its_private_method_debug | ||
rspec.add_setting :its_raise_errors_for_private_method_calling | ||
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. Keeping in mind there's |
||
|
||
rspec.extend RSpec::Its | ||
rspec.backtrace_exclusion_patterns << %r(/lib/rspec/its) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This will be quite annoying. I would expect to see a deprecation message when I'm testing a private method and there's really a difference between using
send
andpublic_send
.Please also check usages of
deprecate
, its interface is not too friendly, but surely you'll find an example that would fit this usage scenario.