Rubocop-rspec: RSpec/ScatteredLet unexpected behavior on let(:x, &proc)

Created on 17 May 2019  Â·  2Comments  Â·  Source: rubocop-hq/rubocop-rspec

I've come to a weird use of let today:

shared_examples 'validate event_type' do |args|
  ...
  let(:expected_event_type_to_use, &args[:event_type_to_use])
  ...
end

...
it_behaves_like 'validate event_type', event_type_to_use: -> { event_type_sub.id }
...

Not going to discussion if this was a good use or not, it just works. RuboCop is flagging it wrongly though: it seems that it does not flag it as let and therefore all the lets below it are flagged by RSpec/ScatteredLet:

159:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.
 164:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.
 165:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.
 180:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.
 185:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.
 190:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.
 195:7  warning  rubocop:C  RSpec/ScatteredLet: Group all let/let! blocks in the example group together.

Version info:

⋊> ruby -v
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]
⋊> rails -v
Rails 5.2.3
⋊> bundle exec rubocop -v
0.64.0
bug

Most helpful comment

IMO, we should fix it.

We could independently consider adding a cop to discourage doing this though if there was a consensus around that.

All 2 comments

This is an interesting trick.
Would you consider using a more canonical alternative?

shared_examples 'validate event_type' do
  # code using `expected_event_type_to_use`
end

# ...

it_behaves_like 'validate event_type' do
  let(:expected_event_type_to_use) { event_type_sub.id }
end

IMO, we should fix it.

We could independently consider adding a cop to discourage doing this though if there was a consensus around that.

Was this page helpful?
0 / 5 - 0 ratings