Rails, rspec, capybara, webkit / selenium, devise, logged out after each request

I use rspec / capybara in my rails project for tests, which works fine with the default driver. But when I switch to webkit or selenium, I log out after every request that I make. This code works as expected, I see that the logged in page twice:

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 install webkit or selenium as a driver, only the first page is the registered version, on the second page I log 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?

+4
source share
1 answer

, : Capybara ?

, , 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
+1

Source: https://habr.com/ru/post/1609183/


All Articles