rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

Cop idea: use let! to create records

Open Darhazer opened this issue 3 years ago • 4 comments

I've seen specs that just reference let in before to trigger the record creation

let(:something) { create :record }

before { something }

The same should be either accomplished by turning the let into a let!, or if otherwise unreferenced - to set the create inside the before. For the last case we already have a cop that would suggest not to use the let! so it would be fine for a cop to require let! and then another one to complain against it (though might be a bit confusing for the user)

Darhazer avatar Dec 23 '20 12:12 Darhazer

I'm struggling to come up with a name for this cop. Any suggestions?

Darhazer avatar Jan 01 '21 15:01 Darhazer

@Darhazer How does RSpec/ForceEvalLet sound for the name here? Since this is not restricted to just creating records, could be any method call.

tejasbubane avatar May 20 '21 06:05 tejasbubane

name for this cop. Any suggestions?

Or RSpec/LetWarmUp.

pirj avatar Jun 16 '22 23:06 pirj

I'm thinking of how to deal with before and let on different nesting levels.

  1. before is deeper:
let(:foo) { Bar.foo }

context 'with warm-up' do
  before { foo }

  # ...
end

context 'without warm-up' do
  # ...
end

Should we ignore? Or should we recommend removing let(:foo) where it's not referenced? What if it still is references somehow?

  1. let is deeper
before { foo }

context 'one' do
  let(:foo) { Bar.foo }

  # ...
end

context 'two' do
  let(:foo) { Baz.foo }

  # ...
end

It looks like the parent before can be removed, and child lets can be turned into let!s.

@tejasbubane Would you be interested in adding such a cop?

pirj avatar Jun 16 '22 23:06 pirj