Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails feature testing with logged in user (undefined method 'session')

I'm writing feature specs with rspec and want to skip the login step each time. Users are authenticated through omniauth (no devise) and a session[:user] variable is set and then referenced to stay logged in. I want to be able to set the session variable directly if possible:

require 'spec_helper'

describe "My page" do
  it "has what I want on it when logged in" do
    user = FactoryGirl.create(:user)
    session[:user] = user.id
    visit my_path
    page.should have_content "Foobar"
  end
end

but this gives

 Failure/Error: session[:user] = user.id
 NameError:
   undefined local variable or method `session' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fd13456d170>

Is there any way to set the session variable directly so that I can avoid the login step in most of the tests?

like image 288
Tyler Avatar asked Dec 09 '13 23:12

Tyler


1 Answers

The rack_session_access gem seems to work well: https://github.com/railsware/rack_session_access

With it, the test becomes:

require 'spec_helper'

describe "My page" do
  it "has what I want on it when logged in" do
    user = FactoryGirl.create(:user)
    page.set_rack_session(user: user.id)
    visit my_path
    page.should have_content "Foobar"
  end
end
like image 69
Tyler Avatar answered Sep 19 '22 23:09

Tyler