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

RFE: detect overriden let() helpers in Rspec tests #3769

Closed
atodorov opened this issue Dec 4, 2016 · 2 comments
Closed

RFE: detect overriden let() helpers in Rspec tests #3769

atodorov opened this issue Dec 4, 2016 · 2 comments

Comments

@atodorov
Copy link

atodorov commented Dec 4, 2016

Expected behavior

RuboCop will warn me when let() helper has been defined twice inside my rspec examples

Actual behavior

The following piece of RSpec code will produce a segmentation fault. See
Ruby #10460.

# frozen_string_literal: true
describe 'my attempt' do
  let(:user) { 'user 1' }
  let!(:client) { 'Client ' + user }
  let(:user) { client.lower }

  it 'some tests' do
    p user
  end
end

However RuboCop doesn't detect that as an offence. In practice the problem was hidden under layers of shared examples and became obvious after I expanded the code to see what it does. Sounds like an easy thing to do for RuboCop, doesn't it ?

Steps to reproduce the problem

  1. Save the above file as segfault_spec.rb
  2. rubocop segfault_spec.rb
  3. rspec segfault_spec.rb

RuboCop version

$ rubocop -V
0.46.0 (using Parser 2.3.3.1, running on ruby 2.3.1 x86_64-linux)
@atodorov
Copy link
Author

atodorov commented Dec 4, 2016

With rubocop-rspec added I get:

Inspecting 1 file
C

Offenses:

spec/segfault_spec.rb:2:10: C: RSpec/DescribeClass: The first argument to describe should be the class or module being tested.
describe 'my attempt' do
         ^^^^^^^^^^^^

1 file inspected, 1 offense detected

@backus I guess this falls under rubocop-rspec, let me know if you want me to open the RFE under the other repo ?

@backus
Copy link
Contributor

backus commented Dec 5, 2016

@atodorov this would be more appropriate for rubocop-rspec but I would probably still say that it isn't a great task for static analysis. I have a project (backus/rspec-lint) which I think is better suited for this sort of detection. All it currently does it detect unused let definitions but that is still flakey. It isn't ready for prime time yet but I'd recommend opening the issue there.

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

No branches or pull requests

2 participants