allow activate defined `let` by `let!` without block
Created by: jalkoby
In last few months I have a lot specs of next structure:
describe 'Some specs' do
let(:car) { create :car }
it "spec which doesn't need `car`" do
end
it "spec which need `car` not instantly" do
# some code
car.turn_left
# some code
end
context 'context which needs preloaded car' do
let!(:car) { create :car }
end
context 'another context which needs preloaded car' do
let!(:car) { create :car }
end
end
Above example shows us the duplication. Another solution for this problem might be next
describe 'Some specs' do
let(:car) { create :car }
it "spec which doesn't need `car`" do
end
it "spec which need `car` not instantly" do
# some code
car.turn_left
# some code
end
context 'context which needs preloaded car' do
before { car }
end
context 'another context which needs preloaded car' do
before { car }
end
end
But using before make specs little bit unreadable.
My solution is using let!
without block
describe 'Some specs' do
let(:car) { create :car }
it "spec which doesn't need `car`" do
end
it "spec which need `car` not instantly" do
# some code
car.turn_left
# some code
end
context 'context which needs preloaded car' do
let!(:car)
end
context 'another context which needs preloaded car' do
let!(:car)
end
end
It activates defined let
with lefting specs clean.