gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:53:10 24 4
gpt4 key购买 nike

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

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

问题是我无法编辑 /etc/hosts CI 环境中的文件,因此当我的 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

路线.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 解决了这个问题,它基本上完成了与编辑 /etc/hosts 相同的事情。带有自定义 DNS 的文件。

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

关于ruby-on-rails - 持续集成中的子域 + 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com