我在找出在CircleCI上设置主机/端口以进行测试的好方法时遇到了问题
编辑2 - 要求:
localhost
切换到127.0.0.1
会导致测试失败127.0.0.1
,我希望Capybara连接到该网站而不是直接localhost / 127.0.0.1 www.example-remote.com
端口80 以前我的测试套件运行正常localhost:3042然后我意识到我在使用会话的测试时遇到问题:rails app本身在localhost上启动但是电子邮件被发送到127.0.0.1地址导致基于会话测试失败
我更改了以下配置
# feature/env.rb
Capybara.server_port = ENV['TEST_PORT'] || 3042
Rails.application.routes.default_url_options[:port] = Capybara.server_port
if ENV['CIRCLECI']
Capybara.default_host = 'http://www.example.com/'
end
# configuration/test.rb
config.action_mailer.default_url_options = {
host: (ENV['CIRCLECI'].present? ? 'www.example.com' : '127.0.0.1'),
port: ENV['TEST_PORT'] || 3042
}
# circle.yml
machine:
hosts:
www.example.com: 127.0.0.1
但现在我正在生成像http://www.example.com/:3042/xxx
是否有人使用自定义主机名在circleCI上管理工作配置?
修改
Capybara 2.13 Rails 5.0 黄瓜2.4 CircleCI 1.x
答案 0 :(得分:0)
Capybara.default_host
仅影响使用rack_test驱动程序的测试(仅当Capybara.app_host
未设置时)。它不应该有尾随' /'在它上面,它已经默认为' http://www.example.com'所以你的设置应该是不必要的。
如果您正在尝试做的是让所有测试(JS和非JS)进入' http://www.example.com'默认情况下你应该能够做到
Capybara.server_host = 'www.example.com'
或
Capybara.app_host = 'http://www.example.com'
Capybara.always_include_port = true
答案 1 :(得分:0)
我的新配置似乎适用于基于会话的测试但远程网站失败(它尝试使用我定义的相同TEST_PORT到达远程服务器(例如,点击带有http://www.example-remote.com/some_path
的电子邮件 - > Capybara连接到http://www.example-remote.com:TEST_PORT/some_path
)
# features/env.rb
# If test port specified, use it
if ENV['TEST_PORT'].present?
Capybara.server_port = ENV['TEST_PORT']
elsif ActionMailer::Base.default_url_options[:port].try do |port|
Capybara.server_port = port
end
else
Rails.logger.warn 'Capybara server port could not be inferred'
end
# Note that Capybara needs either an IP or a URL with http://
# Most TEST_HOST env variable will only include domain name
def set_capybara_host
host = [
ENV['TEST_HOST'],
ActionMailer::Base.default_url_options[:host]
].detect(&:present?)
if host.present?
# If the host is an IP, Capybara.app_host = IP will crash so do nothing
return if host =~ /^[\d\.]+/
# If hostname starts with http(s)
if host =~ %r(^(?:https?\:\/\/)|(?:\d+))
# OK
elsif Capybara.server_port == 443
host = 'https://' + host
else
host = 'http://' + host
end
puts "Attempting to set Capybara host to #{host}"
Capybara.app_host = host
else
Rails.logger.warn 'Capybara server host could not be inferred'
end
end
set_capybara_host
# config/environments/test.rb
Capybara.always_include_port = true
config.action_mailer.default_url_options = {
host: (ENV['TEST_HOST'].present? ? ENV['TEST_HOST'] : '127.0.0.1'),
port: (ENV['TEST_PORT'].present? ? ENV['TEST_PORT'] : 3042)
}