rubocop-rspec: Bug: false positive of RSpec/LetSetup with context and outer shared_examples
This issue produces false positives of Rspec/LetSetup with the latest rubocop-rspec (1.36.0) when we put let! inside some context and we also have shared_examples referencing that let! outside the context.
Something like this:
shared_examples 'some shared example' do
it 'checks something' do
is_expected.to eq user
end
end
context 'when inside some context' do
let!(:user) { create :user }
include_examples 'some shared example'
end
If we are to run rubocop-rspec on the code above it would produce the following:
spec/test_spec.rb:8:3: C: RSpec/LetSetup: Do not use let! to setup objects not referenced in tests.
let!(:user) { create :user }
^^^^^^^^^^^
1 file inspected, 1 offense detected
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 15
- Comments: 32 (16 by maintainers)
Commits related to this issue
- Add reports:person_escort_record_quality This adds a new Rake task which outputs a CSV of the person escort record quality, using the `Reports::PersonEscortRecordQuality` class. I've put it in a rep... — committed to ministryofjustice/hmpps-book-secure-move-api by thomasleese 5 years ago
- Add reports:person_escort_record_quality This adds a new Rake task which outputs a CSV of the person escort record quality, using the `Reports::PersonEscortRecordQuality` class. I've put it in a rep... — committed to ministryofjustice/hmpps-book-secure-move-api by thomasleese 5 years ago
I’m fairly certain this isn’t limited to shared examples.
If you have a top-level subject and create an object inside a context with a
let!, you will get the same false positive.I.e.
I’m using 1.42.0 and it’s giving me it for that example (when I remove the rubocop:disable marker):
This is prone to incidental state. subject/let memoize their result, and won’t run twice, which immediately makes them a bad fit for some cases, e.g.
There’s no point in abusing this mechanism, define a helper method:
Also mind implicit block expectations.
I’ve hit this when using
let!inshared_context, which is the reverse situation.