简体   繁体   中英

rails, rspec, capybara, webkit/selenium, devise, logged out after every request

i'm using rspec/capybara in my rails project for tests, which is working fine with the default driver. But when i switch to webkit or selenium i get logged out after every request that i make. This code is working as expected, i see the logged in page 2 times:

require 'rails_helper'

feature 'test' do
  scenario 'this' do
    user = FactoryGirl.create :user
    login_as(user)
    visit root_path
    save_and_open_page
    visit root_path
    save_and_open_page
  end
end

When i set webkit or selenium as driver only the first page is the logged in version, on the second page i'm logged out:

require 'rails_helper'

feature 'test' do
  scenario 'this', driver: :webkit do
    user = FactoryGirl.create :user
    login_as(user)
    visit root_path
    save_and_open_page
    visit root_path
    save_and_open_page
  end
end

How can i fix this?

I was having this exact same problem and eventually came across this question with pretty much the same problem: Why is Capybara discarding my session after one event?

The solution is to include the snippet found here in your rails_helper

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM