2017-04-23 60 views
0
配置主機/端口與黃瓜/水豚

我必須找出好辦法來設置主機/端口的測試上CircleCI導軌上正確CircleCI

EDIT 2個問題 - 要求:

  • 的Rails應用上TEST_PORT本地運行(如果ENV變量可用)或默認端口3000
  • 我有一個基於會話的測試,從localhost如此神奇地切換到127.0.0.1會導致測試失敗
  • 在我CircleCI ENVIRO註釋我將主機www.example.com映射到127.0.0.1,我希望Capybara連接到該網站而不是直接本地主機/ 127.0.0.1
  • 在我的CircleCI環境中,端口80被保留,所以rails應用程序必須運行不同的端口(如3042)
  • 一些集成測試需要連接到遠程站點(對不起沒有VCR還)www.example-remote.com端口80

以前我的測試套件運行良好與本地主機:3042,但隨後我意識到我在使用會話的測試中遇到了問題:rails應用程序本身在本地主機上啓動,但隨後將電子郵件發送到127.0.0.1廣告這導致基於會話的測試失敗

禮服我改變了以下配置類似http://www.example.com/:3042/xxx

產生

# 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 

但現在我越來越怪異的電子郵件網址是否有人管理上circleCI一個工作配置使用自定義主機名?

EDIT

水豚2.13 滑軌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 
0

我的新配置似乎適用於基於會話的測試,但遠程網站失敗(它試圖通過我定義的相同TEST_PORT到達遠程服務器(例如點擊電子郵件http://www.example-remote.com/some_path - >水豚連接到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) 
    }