【问题标题】:Rails correctly configure host/port with Cucumber/Capybara on CircleCIRails 在 CircleCI 上使用 Cucumber/Capybara 正确配置主机/端口
【发布时间】:2017-09-20 12:01:38
【问题描述】:

我无法找到设置主机/端口以在 CircleCI 上进行测试的好方法

编辑 2 - 要求:

  • 在 TEST_PORT(如果在 ENV 变量中可用)或默认端口 3000 上本地运行的 Rails 应用程序
  • 我有基于会话的测试,所以神奇地从 localhost 切换到 127.0.0.1 会导致测试失败
  • 在我的 CircleCI 环境中,我将主机 www.example.com 映射到 127.0.0.1,我希望 Capybara 连接到该网站,而不是直接连接到 localhost/127.0.0.1
  • 在我的 CircleCI 环境中,端口 80 被保留,因此 rails 应用程序必须在不同的端口(如 3042)上运行
  • 一些集成测试需要连接到远程网站(抱歉还没有 VCR)www.example-remote.com在端口 80

以前我的测试套件在 localhost:3042 上运行良好,但后来我意识到我在使用 session 的测试时遇到了问题:rails 应用程序本身在 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

但现在我正在生成奇怪的电子邮件 URL,例如 http://www.example.com/:3042/xxx

是否有人使用自定义主机名在 circleCI 上管理工作配置?

编辑

水豚 2.13 导轨 5.0 黄瓜 2.4 CircleCI 1.x

【问题讨论】:

  • 你用的是什么版本的水豚?

标签: ruby-on-rails cucumber capybara ruby-on-rails-5 circleci


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 2018-06-06
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 2017-06-29
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多