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