In RSpec, using let variable inside before :all block In RSpec, using let variable inside before :all block ruby ruby

In RSpec, using let variable inside before :all block


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.


I'm not sure what you mean by "this" in "what is the correct way of doing this"? Accessing let and subject within a before(:all) is deprecated and will be removed in RSpec 3 with the following explanation from https://github.com/rspec/rspec-core/pull/857:

let and subject declarations are not intended to be called in a before(:all) hook, as they exist to define state that is reset between each example, while before(:all) exists to define state that is shared across examples in an example group.


Taking into account Peter's answer I think the correct way is to access the user in a before each block:

before(:each) do  sign_in userend