ruby-on-rails – rails,rspec,capybara,webkit / selenium,devise,在每次请求后退出

我在我的rails项目中使用rspec / capybara进行测试,这对于默认驱动程序工作正常.但是当我切换到webkit或selenium时,我会在每次发出请求后退出.

此代码按预期工作,我看到登录页面2次:

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

当我将webkit或selenium设置为驱动程序时,第一页是登录版本,在第二页上我已注销:

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

我怎样才能解决这个问题?

最佳答案 我有这个完全相同的问题,并最终遇到了几乎相同的问题:
Why is Capybara discarding my session after one event?

解决方案是在您的rails_helper中包含here的代码段

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
点赞