我花了很多时间研究这个问题,并发现了一些关于这个问题的有趣的博客文章,但没有一个解决方案对我有用。
以下是我尝试过的选项:
我从 Capybara Selenium 的默认 js 驱动程序切换到 Webkit 和 Poltergeist,如下所示:
# Gemfile
gem "capybara-webkit"
# spec/spec_helper.rb
Spork.prefork do
Capybara.javascript_driver = :webkit
end
和
# Gemfile
gem "poltergeist"
# spec/spec_helper.rb
Spork.prefork do
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
end
但也没有运气。
根据这个线程和这篇文章我试过:
# spec/spec_helper.rb
Spork.prefork do
Capybara.server_boot_timeout = 600 # Default is 10 my entire suite
end # takes ~550s to run, that's why I
# attempted such a large boot timeout in
# case the time was from beginning of suite
# execution.
无济于事。
然后我找到了这篇文章,所以我尝试:
# spec/spec_helper.rb
# initial advice was for cucumber, and thus recommended this to be placed in
# the features/env.rb file
def find_available_port
server = TCPServer.new('127.0.0.1', 0)
server.addr[1]
ensure
server.close if server
end
if ENV['TDDIUM'] then
Capybara.server_port = find_available_port
end
但没有运气。
根据 StackOverflow 上的这个问题,我还检查了我的 database_cleaner 设置,以确保 DatabaseCleaner 与 FactoryGirl 的工厂很好地配合使用。
仍然没有运气。
接下来,我尝试从 Guardfile 中较低级别的 rspec 测试中解析出我的 capybara 测试,如下所示:
group 'integration tests' do
# Capybara Tests
guard 'rspec', spec_paths: ['spec/requests'] do
watch(%r{^spec/requests/.+_spec\.rb})
end
# Cucumber Feature Tests
guard 'cucumber', bundler: true do
watch(%r{^features/.+\.feature$})
end
end
group 'unit tests' do
rspec_paths = ['spec/controllers', 'spec/helpers', 'spec/models', 'spec/views']
# RSpec Unit Tests
guard 'rspec', spec_paths: rspec_paths do
watch(%r{^spec/.+_spec\.rb$})
end
# Jasmine JS Unit Tests
guard 'jasmine', all_on_start: false, all_after_pass: false do
watch(%r{spec/javascripts/.+_spec\.(js\.coffee|js|coffee)$})
end
end
成功!最后!