我有一个RSpec功能规范,用于测试我的应用的登录信息。当我在带有Capybara的RSpec中运行它时它会通过,但是当我尝试使用Capybara-webkit运行它标记为js: true
时,它会失败。这是一个问题,因为我的整个应用程序都在登录后面,如果我无法运行这一点,我不知道如何为应用程序的其余部分执行功能规范。
以下是我的尝试:
database_cleaner.rb
文件位于下方。xvfb-run -a bin/rspec spec/features/log_in_spec.rb
(与使用Headless正常运行没什么不同)如何让我的登录规范在Capybara-webkit下运行?我的一些规范需要为JS标记,有些则不会,但是他们都需要用户登录。谢谢。
log_in_spec.rb
require 'rails_helper'
RSpec.feature "Log in", type: :feature do
scenario "as admin" do
user = create(:admin)
# Tried this instead of with Capybara, works with Capybara but not capybara-webkit
# login_as user, scope: :user, run_callbacks: false
visit root_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
find('.btn-primary').click
expect(page).to have_content('Admin')
end
end
spec_helper.rb
require 'capybara/rspec'
require 'paperclip/matchers'
RSpec.configure do |config|
Capybara.javascript_driver = :webkit
Capybara.app_host = 'https://192.168.99.101'
config.include Paperclip::Shoulda::Matchers
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
mocks.verify_doubled_constant_names = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = 'doc'
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 'spec_helper'
require 'rspec/rails'
require 'capybara/rails'
require 'devise'
require 'support/controller_macros'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migrations 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.use_transactional_fixtures = false
config.filter_rails_from_backtrace!
config.include Warden::Test::Helpers
config.before :suite do
Warden.test_mode!
end
config.after :each do
Warden.test_reset!
end
end
# Added headless gem and this code thanks to this post: http://stackoverflow.com/a/28706535/3043668
if ENV['HEADLESS']
require 'headless'
headless = Headless.new
headless.start
at_exit { headless.stop }
end
规格/支持/ database_cleaner.rb
RSpec.configure do |config|
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
规格/支持/ headless.rb
RSpec.configure do |config|
config.around type: :feature do |example|
Headless.ly do
example.run
end
end
end
规格/支持/ capybara.rb
Capybara::Webkit.configure do |config|
config.debug = true
config.allow_unknown_urls
config.timeout = 5
config.ignore_ssl_errors
config.skip_image_loading
end
这是我运行测试时debug output from Capybara-webkit的要点。看起来它一遍又一遍地尝试同样的事情。
更新
我删除了Capybara.app_host
设置,非JS测试仍然通过,但是当我在capybara-webkit下运行时,我在debig输出中看到了这一点:
Received 0 from "https://127.0.0.1:37193/login"
Page finished with false
Load finished
Page load from command finished
Wrote response false "{"class":"InvalidResponseError","message":"Unable to load URL: http://127.0.0.1:37193/login because of error loading https://127.0.0.1:37193/login: Unknown error"}"
Received "Reset()"
Started "Reset()"
undefined|1|SecurityError: DOM Exception 18: An attempt was made to break through the security policy of the user agent.
正在尝试visit("/login")
并且它正被重定向到https版本,这使它失败了。我如何让它成功?
答案 0 :(得分:1)
第一个原因是保存程序后的记录c =在普通视图中不保存密码,但在这种情况下似乎是好的:
user = create(:admin)
# ...
user = User.first # wrong way
#...
fill_in 'Password', with: 'password'
登录失败的第二个原因是工厂女孩和水豚使用单独的连接,因此在一个会话中创建的数据在另一个会话中不可用。要修复它,请使用单个连接补丁(将其置于规范/支持),如here所述。
答案 1 :(得分:0)
这很难。但问题是我在我的application.rb中设置了force_ssl = true
,愚蠢地,而不是像普通人一样将它放在production.rb和development.rb中。
我也设置了Capybara-webkit' app_host
,事实证明,我不需要这样做。删除之后,运行带调试的capybara-webkit,我发现它正试图从http://localhost:45362/login
(或任何端口)重定向到https://localhost:45362/login
(注意https
!)这导致了DOM 18安全性错误或其他任何问题,这让它变得窒息。我关掉了force_ssl
,现在它就像一个冠军。希望这可以帮助你不要撕掉你的头发。