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

False negative for RSpec/RepeatedExampleGroupDescription #2021

Open
ydakuka opened this issue Jan 11, 2025 · 6 comments
Open

False negative for RSpec/RepeatedExampleGroupDescription #2021

ydakuka opened this issue Jan 11, 2025 · 6 comments

Comments

@ydakuka
Copy link

ydakuka commented Jan 11, 2025

Describe the solution you'd like

RSpec.describe Screenshots::CreateInteractor do
  context 'when the request is valid', use_vcr_cassette: 'screenshots/valid_request' do
    pending 'add something'
  end

  context 'when the request is valid', use_vcr_cassette: 'screenshots/invalid_request' do
    pending 'add something'
  end
end

RuboCop

ydakuka@yauhenid:~/Work/project$ rubocop -V
1.70.0 (using Parser 3.3.6.0, rubocop-ast 1.37.0, analyzing as Ruby 2.7, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-performance 1.23.1
  - rubocop-rails 2.28.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 3.3.0
  - rubocop-rspec_rails 2.30.0
  - rubocop-thread_safety 0.6.0
@ydakuka ydakuka changed the title False negative on the RSpec/RepeatedExampleGroupDescription cop False negative for RSpec/RepeatedExampleGroupDescription Jan 11, 2025
@pirj
Copy link
Member

pirj commented Jan 11, 2025

Shouldn't this be?

-  context 'when the request is valid', use_vcr_cassette: 'screenshots/invalid_request' do
+  context 'when the request is invalid', use_vcr_cassette: 'screenshots/invalid_request' do

@ydakuka
Copy link
Author

ydakuka commented Jan 11, 2025

Sorry for the lack of explanation. No, it should work the same way as if it don't contain any metadata. P.S. And by accident, I combined two false negatives into one.

Correct behavior:

For instance,

RSpec.describe Screenshots::CreateInteractor do
  context 'when the request is valid' do
    specify do
      expect(true).to be_truthy
    end
  end

  context 'when the request is valid' do
    specify do
      expect(false).to be_falsey
    end
  end
end

I run rubocop:

ydakuka@yauhenid:~/Work/project$ rubocop spec/interactors/screenshots/create_interactor_spec.rb 
Inspecting 1 file
C

Offenses:

spec/interactors/screenshots/create_interactor_spec.rb:6:3: C: RSpec/RepeatedExampleGroupDescription: Repeated context block description on line(s) [12]
  context 'when the request is valid' do ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/interactors/screenshots/create_interactor_spec.rb:12:3: C: RSpec/RepeatedExampleGroupDescription: Repeated context block description on line(s) [6]
  context 'when the request is valid' do ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

False negatives

  1. Rubocop doesn't detect any offenses if context blocks contain metadata in at least one test case.
RSpec.describe Screenshots::CreateInteractor do
  context 'when the request is valid', use_vcr_cassette: 'screenshots/valid_request' do
    specify do
      expect(true).to be_truthy
    end
  end

  context 'when the request is valid' do
    specify do
      expect(false).to be_falsey
    end
  end
end

or

RSpec.describe Screenshots::CreateInteractor do
  context 'when the request is valid', use_vcr_cassette: 'screenshots/valid_request' do
    specify do
      expect(true).to be_truthy
    end
  end

  context 'when the request is valid', use_vcr_cassette: 'screenshots/invalid_request' do
    specify do
      expect(false).to be_falsey
    end
  end
end

I run rubocop, and there are no offenses. Rubocop finds the RSpec/RepeatedExampleGroupDescription offense only if context blocks contain the same name (description) and metadata.

Rubocop doesn't detect the RSpec/RepeatedExampleGroupDescription offense if it contains pending.

RSpec.describe Screenshots::CreateInteractor do
  context 'when the request is valid' do
    pending 'add something'
  end

  context 'when the request is valid' do
    pending 'add something'
  end
end

@pirj
Copy link
Member

pirj commented Jan 12, 2025

  1. What is different in those two cassettes? Why the context description doesn’t reflect that difference for the reader? How the reader is supposed to understand that difference just by looking at the “document” RSpec output? So far, I fail to see a convincing case to have two contexts with identical descriptions that are (supposedly) different somehow.

@pirj
Copy link
Member

pirj commented Jan 12, 2025

  1. A PR is welcome! However, the only pain I can see coming from not having this is a copy-pasta with some lost context that will be hard to restore in the future. So it’s a nice to have fix, but not something I would focus on.

@ydakuka
Copy link
Author

ydakuka commented Jan 16, 2025

  1. Only the names are different. The context description does not reflect the difference because there is the copy-paste mistake present. This false negative should be detected by the cop.

@pirj
Copy link
Member

pirj commented Jan 17, 2025

  1. The cop does detect this copy-paste. It’s what it does. It has no intention to check if the docstring matches the code of example group setup.

  2. If you feel like you want to fix the case with pending, please send a PR

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