rubocop-rspec
rubocop-rspec copied to clipboard
Cop idea: use let! to create records
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)
I'm struggling to come up with a name for this cop. Any suggestions?
@Darhazer How does RSpec/ForceEvalLet
sound for the name here? Since this is not restricted to just creating records, could be any method call.
name for this cop. Any suggestions?
Or RSpec/LetWarmUp
.
I'm thinking of how to deal with before
and let
on different nesting levels.
-
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?
-
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 let
s can be turned into let!
s.
@tejasbubane Would you be interested in adding such a cop?