Skip to content

allow activate defined `let` by `let!` without block

gitlab-qa-bot requested to merge github/fork/jalkoby/activate_defined_let into master

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.

Merge request reports