Use variable throughout every scenario in feature spec Capybara

Viewed 29

So I have this feature spec:

require 'rails_helper'

RSpec.feature "Catalog interactions", type: :feature do

    let(:user) { FactoryBot.create(:non_admin_user) }

    # To give each product in the list a different rating
    let!(:product) do 
        FactoryBot.create_list(:correct_product, 2).each_with_index do |product, i|
            product.update(rating: i + 1) #need to do update so that the data persists
        end
    end

    scenario "1" do

    end

    scenario "2" do

    end

    scenario "3" do

    end

    scenario "4" do
       
    end
end

Now I did let!(:product) in order to create 2 products for the 4 scenarios I have.

But after debugging, it seems it is creating 2 products for each of the scenarios. So for scenario 1 would be product id:1 and id:2 for scenario 2 would be product id:3 and id:4 and so on.

This takes quite a lot of time so I was wondering if there is a way to do it properly and just have it create 2 products and use them for every scenario in this file.

Thanks!

0 Answers
Related