我已成功添加在我的应用程序中使用动态子域的功能。问题是,当我运行 Cucumber 测试时,当我的应用程序执行包含子域的 redirect_to 时,我收到以下错误:
features/step_definitions/web_steps.rb:27
the scheme http does not accept registry part: test_url.example.com (or bad hostname?)
我有一个注册控制器操作,它创建用户和选择的帐户,并将用户重定向到注销方法,并根据用户在注册表单中选择的子域指定子域。这是创建和保存用户和帐户模型后发生的重定向操作的代码:
redirect_to :controller => "sessions", :action => "destroy", :subdomain => @account.site_address
这是我的rails 3条路线:
constraints(Subdomain) do
resources :sessions
match 'login', :to => 'sessions#new', :as => :login
match 'logout', :to => 'sessions#destroy', :as => :logout
match '/' => 'accounts#show'
end
这是到目前为止我在上面的约束中指定的 Subdomain 类的代码:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
我将 UrlHelper 添加到 ApplicationController:
class ApplicationController < ActionController::Base
include UrlHelper
protect_from_forgery
end
这是上述 UrlHelper 类的代码:
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain, request.port_string].join
end
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
上面的所有代码都允许我在本地浏览器中正常运行子域。当我运行 Cucumber 测试时,会发生上述问题。测试单击注册按钮,该按钮又调用 redirect_to 并引发上面列出的异常。
这是我的 gem 文件的样子:
require 'subdomain'
SomeApp::Application.routes.draw do
resources :accounts, :only => [:new, :create]
match 'signup', :to => 'accounts#new'
constraints(Subdomain) do
resources :sessions
match 'login', :to => 'sessions#new', :as => :login
match 'logout', :to => 'sessions#destroy', :as => :logout
match '/' => 'accounts#show'
end
end
您能否告诉我另一种让我的测试现在工作的方法?我会对修复或可以在不使用子域的情况下测试我的方法的方式感兴趣(例如,检索帐户名称的模拟方法)。