铁轨在Circleci上正确配置带有Cucumber/Capybara的主机/端口



我在弄清楚在CircleCi上设置主机/端口的好方法

编辑2-要求:

  • Rails应用程序在test_port上本地运行(如果在Env变量中可用(或默认端口3000
  • 我有基于会话的测试,因此神奇地从localhost切换到127.0.0.1会导致测试失败
  • 在我的CircleCi环境上,我将主机绘制为www.example.com到127.0.0.1,我希望Capybara可以连接到该网站,而不是直接Localhost/127.0.0.0.1
  • 在我的Circleci环境中,端口80保留了,因此Rails应用必须在其他端口上运行(例如3042(
  • 一些集成测试需要连接到远程网站(对不起,尚无VCR( www.example-remote.com 在端口80

以前我的测试套件在Localhost中运行良好:3042,但后来我意识到我在使用Session的测试中遇到问题: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

一样生成怪异的电子邮件URL

有人使用自定义主机名在CircleCi上管理工作配置吗?

编辑

Capybara 2.13铁轨5.0黄瓜2.4Circleci 1.x

Capybara.default_host仅使用rack_test驱动程序影响测试(仅当未设置Capybara.app_host时(。它不应该在上面落后'/',并且已经默认为'http://www.example.com',因此您的设置不必要。

如果您要做的是进行所有测试(JS和NON-JS(,请默认访问'http://www.example.com',那么您应该可以做

Capybara.server_host = 'www.example.com'

Capybara.app_host = 'http://www.example.com'
Capybara.always_include_port = true

我的新配置似乎适用于基于会话的测试,但对于远程网站而言失败(它试图使用我定义的相同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)
  }

最新更新