我正在使用Capybara和RSpec将feature specs放在现有的Rails应用程序中。默认情况下,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时遇到运行此测试的问题(通过在Capybara.default_driver :webkit
中设置spec_helper.rb
)。出于某种原因,我在日志中看到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)
答案 0 :(得分:1)
我认为您可能需要使用Warden::Test::Helpers
代替as described here
请注意,他们也有一个特定的部分来处理capybara-webkit
。
答案 1 :(得分:0)
401可能意味着密码与您用于登录的电子邮件不匹配。由于它使用rack_test而不是capybara-webkit为您工作,因此您很可能仍在使用基于事务的测试。
使用rack_test时,应用程序在与测试相同的线程中运行,这意味着它们都使用相同的数据库连接。因此,您可以使用基于事务的测试,而不必支付实际向数据库写入任何内容的性能损失。但是当使用任何真正的浏览器时#34;驱动程序Capybara在其自己的线程中启动您的应用程序,这意味着测试和应用程序使用单独的数据库连接,因此您必须实际提交到数据库。这通常通过在截断模式下使用database_cleaner来完成。见https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example