In RSpec, using let variable inside before :all block

Fabian Silva picture Fabian Silva · Oct 29, 2013 · Viewed 18.4k times · Source

I have the following code inside most of my tests:

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

 before do
   sign_in user
   visit products_path
 end
...
end

But I'm getting the following warning:

WARNING: let declaration 'user' accessed in a 'before(:all)'

My question is, what is the correct way of doing this? I can't find much information about the warning itself.

Thanks!

EDIT: My goal is to use the user variable so I can pass it on to sign_in, which signs the user in, and use it later on another tests (I check for the company attribute of the User)

Answer

Romain Paulus picture Romain Paulus · Oct 30, 2013

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.