Capybara在一次访问后没有召开用户会话 - Rails 4.2

时间:2016-02-24 18:04:56

标签: ruby-on-rails rspec capybara database-cleaner

我无法让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

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

1 个答案:

答案 0 :(得分:1)

快速猜测你的user = FactoryGirl.create(:user)需要在你的前一个块中 - 否则它只会在加载这个特征时运行一次 - 然后在第一个特征运行之前从数据库中删除