ruby-on-rails – 持续集成中的子域集成测试

我正在尝试使用Codeship持续集成,它运行我的所有selenium /集成测试.

我的Rails应用程序在routes.rb文件中有一些特殊的子域逻辑,我在下面列举了一个例子.

问题是我无法在CI环境中编辑/ etc / hosts文件,所以当我的Rspec suti运行并尝试使用我的UrlHelper模块访问子域时,无法访问服务器.例如,它尝试联系subdomain.localhost:3000

有关如何处理CI中这些特定于子域​​的URL的任何想法?

来自要点的代码:

domain_constraint.rb

class DomainConstraint
  def initialize(domain)
    @domains = [domain].flatten
  end

  def matches?(request)
    request.subdomain.present? ? domain_to_match = request.subdomain + "." + request.domain : domain_to_match = request.domain
    @domains.include? domain_to_match
  end
end

integration_spec.rb

require 'spec_helper'

describe "Something", js: true do
  it "is a fake spec" do
    # this won't work in CI/test environments
    # 'another.localhost:3001' is not mapped in /etc/hosts
    visit foo_path(subdomain: 'another')
  end
end

的routes.rb

MyApp::Application.routes.draw do
  constraints DomainConstraint.new(['subdomain.domain.com', 'another.domain.com']) do
    resources :foo
  end
end

url_helper.rb

module UrlHelper
  def with_subdomain(subdomain)
    subdomain = (subdomain || "")
    subdomain += "." unless subdomain.empty?
    [subdomain, request.domain].join
  end

  # allow link_to :subdomain => ""
  def url_for(options = nil)
    if options.kind_of?(Hash) && options.has_key?(:subdomain)
      options[:host] = with_subdomain(options.delete(:subdomain))
    end
    super
  end
end

最佳答案 我通过使用xip.io来解决这个问题,它基本上完成了与使用自定义DNS编辑/ etc / hosts文件相同的功能.

http://www.chrisaitchison.com/2013/03/17/testing-subdomains-in-rails/

点赞