Capybara 在访问一个页面后不保持用户会话 -- Rails 4.2

Capybara not holding user session after one page visit -- Rails 4.2

我无法让 Capybara 在一个规范内进行超过一页访问的会话。我很熟悉 Capybara 的驱动程序可能正在使用不同的数据库连接的概念,但我所做的任何事情(包括不使用 :js => true)似乎都没有改变任何东西。

我只是想访问一个需要用户身份验证的页面。它在第一次访问时成功。当我第二次访问它时失败了。 (这当然在我手动测试开发时都有效。)代码:

activity_areas_spec.rb

require "rails_helper"

RSpec.feature "Activity areas", :type => :feature do
  
  user = FactoryGirl.create(:user)
  before(:each) do
    login_as(user, :scope => :user)
  end
  after(:each) do
    Warden.test_reset!
  end
  
  ...

  feature "displays activities shared", :js => true do
    scenario "to public" do
      visit area_activities_path
      expect(page).to have_content I18n.t('pages.activity.areas.title') #passes
      visit area_activities_path
      expect(page).to have_content I18n.t('pages.activity.areas.title') #fails -- user is redirected to login page.
    end
  end
end

#rails_helper.rb
    ENV['RAILS_ENV'] ||= 'test'
    require File.expand_path('../../config/environment', __FILE__)
    # Prevent database truncation if the environment is production
    abort("The Rails environment is running in production mode!") if Rails.env.production?
    require 'rspec/rails'
    require 'spec_helper'
    require 'capybara/rails'
    require 'devise'
    require 'support/controller_macros'
    
    include Warden::Test::Helpers
    Warden.test_mode!
    
    ActiveRecord::Migration.maintain_test_schema!
    
    RSpec.configure do |config|
      # Factory girl
      config.include FactoryGirl::Syntax::Methods
      # Make routes available in specs
      config.include Rails.application.routes.url_helpers
      # Capybara
      config.include Capybara::DSL
      # To get Devise test helpers
      config.include Devise::TestHelpers, :type => :controller
      config.extend ControllerMacros::DeviseHelper, :type => :controller
      config.include ControllerMacros::AttributeHelper, :type => :controller
       
      config.use_transactional_fixtures = false
      config.infer_spec_type_from_file_location!
      config.filter_rails_from_backtrace!
      
      # Database cleaner gem configurations
      config.before(:suite) do
        DatabaseCleaner.clean_with(:truncation)
      end
      config.before(:each) do
        DatabaseCleaner.strategy = :transaction
      end
      config.before(:each, :js => true) do
        DatabaseCleaner.strategy = :truncation
      end
      config.before(:each) do
        DatabaseCleaner.start
      end
      config.after(:each) do
        DatabaseCleaner.clean
      end
      
    end

因为我对 Capy 可能使用不同的数据库连接这一事实很敏感,所以我尝试按照 DatabaseCleaner 的建议代码代替上述 DatabaseCleaner 命令:

config.before(:suite) do
    if config.use_transactional_fixtures?
      raise(<<-MSG)
        Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
        (or set it to false) to prevent uncommitted transactions being used in
        JavaScript-dependent specs.

        During testing, the app-under-test that the browser driver connects to
        uses a different database connection to the database connection used by
        the spec. The app's database connection would not be able to access
        uncommitted transaction data setup over the spec's database connection.
      MSG
    end
    DatabaseCleaner.clean_with(:truncation)
  end  

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, type: :feature) do
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    if !driver_shares_db_connection_with_specs
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end

See here.

#spec_helper.rb
    require 'factory_girl_rails'
    require "capybara/rspec"
    require 'sidekiq/testing'
    RSpec.configure do |config|
      config.expect_with :rspec do |expectations|
        expectations.include_chain_clauses_in_custom_matcher_descriptions = true
      end
        
      config.mock_with :rspec do |mocks|
        mocks.verify_partial_doubles = true
      end
    end

我很容易感到困惑,非常感谢以简单的方式提供的任何帮助。如果您需要任何其他文件,请告诉我。

作为快速猜测,您的 user = FactoryGirl.create(:user) 需要在您的 before 块内 - 否则它只会在加载功能时 运行 一次 - 然后在第一个功能之前从数据库中删除是 运行