切换 Capybara 默认驱动程序后出现 401 错误?

401 errors after switching Capybara default driver?

我正在使用 Capybara 和 RSpec 为现有的 Rails 应用整合 feature specs。默认情况下,Capybara 使用 rack_test 作为驱动程序,但我想使用 capybara-webkit.

我整理了一个简单的测试来登录我的站点。使用 rack_test 我可以登录,我的 log/test.log 文件显示以下 HTTP 请求:

Completed 200 OK in 201ms (Views: 201.3ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-12-01 10:20:33 -0600
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"email"=>"foo@bar.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'foo@bar.com'  ORDER BY "users"."id" ASC LIMIT 1
   # There are a few more SQL queries here that I've removed
Redirected to http://www.example.com/
Completed 302 Found in 36ms (ActiveRecord: 2.3ms)
Started GET "/" for 127.0.0.1 at 2015-12-01 10:20:33 -0600
Processing by InitiativesController#blank as HTML

当我切换到 webkit(通过在 spec_helper.rb 中设置 Capybara.default_driver :webkit)时,我在 运行 这个测试中遇到了问题。出于某种原因,我在日志中看到 401 错误:

Completed 200 OK in 17ms (Views: 16.7ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-12-01 10:21:31 -0600
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "user"=>{"email"=>"foo@bar.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'foo@bar.com'  ORDER BY "users"."id" ASC LIMIT 1
  # no other queries
Completed 401 Unauthorized in 5ms
Processing by Devise::SessionsController#new as HTML

所以登录请求没有被接受。是否需要更改某种 Capybara 配置才能使此登录请求正常工作?我不知道是什么原因造成的。

我的 spec_helper.rb 看起来像这样:

require 'simplecov'
require 'webmock/rspec'
require 'capybara/rspec'
require 'fakeredis/rspec'
require 'site_prism'

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

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

Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :webkit
Capybara.default_host = 'http://127.0.0.1:60400'
Capybara.server_port = 60400

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

Capybara::Webkit.configure do |config|
  config.block_unknown_urls
end

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.infer_base_class_for_anonymous_controllers = false

  config.include Devise::TestHelpers, :type => [:controller, :acceptance]

  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do |example|
    DatabaseCleaner.strategy = :truncation 
    DatabaseCleaner.start
  end

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

  config.order = "random"

  config.before(:all) do
    FactoryGirl.reload
  end

  config.before :each do
    Timecop.return
  end

  config.include Helpers
  config.include Helpers::MassAdsCreation
end

WebMock.disable_net_connect!(allow_localhost: true)

401 可能意味着密码与您用于登录的电子邮件不匹配。由于它适用于 rack_test 而不是 capybara-webkit,因此您很可能仍在使用基于事务的测试。
当使用 rack_test 时,应用程序 运行 在与测试相同的线程中,这意味着它们都使用相同的数据库连接。因此,您可以使用基于事务的测试,而不必为实际向数据库写入任何内容而付出性能损失。但是,当使用任何 "real browser" 驱动程序时,Capybara 会在它自己的线程中启动您的应用程序,这意味着测试和应用程序正在使用单独的数据库连接,因此您必须实际提交给数据库。这通常是通过在 t运行cation 模式下使用 database_cleaner 来完成的。参见 https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example

我认为您可能需要使用 Warden::Test::Helpers 而不是 as described here

请注意,他们还有一个特定的部分涉及 capybara-webkit