Capybara在每次请求后重置设计会话

时间:2016-12-11 08:44:06

标签: ruby-on-rails session rspec devise capybara

代码示例:

login_as(user, scope: :user)
visit user_edit_path #this is authorized
# some expectations
visit user_comments_path #this is not authorized, return 401, why?

但如果我在第一次访问后添加第二个 login_as - 一切正常。我不知道为什么设计会话重置。 当我尝试通过 fill_in 登录登录表单并 click_on '登录'时,会话重定向到root_path后重置!因此,在这种情况下,我甚至无法做出用户登录的基本期望。 我测试了不同的驱动程序。

Rspec配置文件:

# This file is copied to spec/ when you run 'rails generate rspec:install'
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 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'

# NOTE: For Chrome support
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

# Capybara.register_driver(:poltergeist) { |app| Capybara::Poltergeist::Driver.new(app, js_errors: false) }
# Capybara.javascript_driver = :poltergeist

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include WaitForAjax
  config.include FeatureHelpers
  config.include Devise::TestHelpers, type: :controller
  config.include Warden::Test::Helpers
  config.extend ControllerHelpers,    type: :controller

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.before :suite do
    Warden.test_mode!
  end
end

1 个答案:

答案 0 :(得分:1)

我必须从config.action_controller.default_url_options = {:host => "localhost:3000"}

中删除environments/test.rb