In RSpec, using let variable inside before :all block

后端 未结 3 2055
眼角桃花
眼角桃花 2021-01-07 16:54

I have the following code inside most of my tests:

describe \'index\'
 let(:company) { FactoryGirl.create(:company) }
 let(:user) { FactoryGirl.create(:user,         


        
3条回答
  •  孤独总比滥情好
    2021-01-07 17:57

    I had the same problem, I have solved it by declaring all my variables as attributes inside the before block:

    describe 'index'
    
     before(:all) do
       @company = FactoryGirl.create(:company)
       @user = FactoryGirl.create(:user, company: @company)
    
       sign_in @user
       visit products_path
     end
    ...
    end
    

    Now you can use @user and @company inside your tests, and you shouldn't have any warnings.

提交回复
热议问题