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

Proposal: Inform user when testing a private method #82

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zirni
Copy link

@zirni zirni commented Mar 17, 2021

Hello guys,

please take a look about this feature proposal
for safely migrate from send to public_send
mentioned #28

I'd like to know if this would be an approach you would accept.
Then I would finalize this little piece.

Cheers

as a migration path to safely move from send to public_send
Gemfile Outdated
@@ -29,3 +29,5 @@ gem 'contracts', '< 0.16' if RUBY_VERSION < '1.9.0'
gem 'coveralls', :require => false, :platform => :mri_20

eval File.read('Gemfile-custom') if File.exist?('Gemfile-custom')

gem 'pry'
Copy link
Member

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.

@@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

lib/rspec/its.rb Outdated
@@ -95,7 +95,7 @@ module Its
#
# # This ...
# describe Array do
# its(:size, :focus) { should eq(0) }
# its(:size, focus: true) { should eq(0) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Author

@zirni zirni Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted it again :)

lib/rspec/its.rb Outdated

# When NilClass: user didnt set the rspec setting
# When FalseClass: skip this block
if RSpec.configuration.its_private_method_debug.is_a? NilClass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSpec.configuration.its_private_method_debug.nil? ?

lib/rspec/its.rb Outdated
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."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use RSpec.deprecate.
I personally wouldn't want to support testing private methods implicitly, and then it would just make sense to issue a deprecation warning in 1.x, and move to public_send in 2.0.

end.new
end

its(:name, :focus) { should eq('Maria') }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JFYI should is going away in RSpec 4.

@zirni
Copy link
Author

zirni commented Mar 19, 2021

Added the deprecation warning as you mentioned.
For a better debugging experience and to give the user
the chance to quickly find private method calling,
I've added the setting its_raise_errors_for_private_method_calling
which explicitly needs to be set to true.

lib/rspec/its.rb Outdated
Comment on lines 4 to 11
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

Copy link
Member

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 and public_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.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping in mind there's configuration.raise_errors_for_deprecations! I'm on the fence if we need this setting, or a deprecation message will be sufficient.

@zirni
Copy link
Author

zirni commented Mar 24, 2021

Now deprecation warnings are thrown up once private method tests were detected, as you mentioned.
Running the test suite outputs 2 deprecations:

Testing private method name is deprecated. Called from /app/spec/rspec/its_spec.rb:46:in `block (4 levels) in <module:RSpec>'.
Testing private method name is deprecated. Called from /app/spec/rspec/its_spec.rb:42:in `block (4 levels) in <module:RSpec>'.

If the user test suite has a bunch of private method tests, the deprecation warnings would grow to a possible annoying size.
I would rather like to see a single deprecation warning for one or more private method tests and optionally raise an error when
its_raise_errors_for_private_method_calling was set.

@pirj
Copy link
Member

pirj commented Mar 25, 2021

grow to a possible annoying size
single deprecation warning

We are printing deprecations in the footer.
There's a limitation for the total number of deprecations https://github.com/rspec/rspec-core/blob/ea8554afd1a2b63677c6593059fa8f2476181deb/lib/rspec/core/formatters/deprecation_formatter.rb#L62

There's also a limit for the number of deprecations of the same kind https://github.com/rspec/rspec-core/blob/ea8554afd1a2b63677c6593059fa8f2476181deb/lib/rspec/core/formatters/deprecation_formatter.rb#L136

I didn't try it, but I would suggest relying on this existing functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants