使用 Cucumber/Capybara 子域的问题
我已成功添加在应用程序中使用动态子域的功能。问题是,当我运行 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
这是迄今为止我在上面的约束中指定的子域类的代码:
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
您能否让我们知道让我的测试现在工作的附加方法?我对修复程序或无需使用子域即可测试我的方法的方法感兴趣(例如,检索帐户名称的模拟方法)。
I have successfully added the ability to use dynamic subdomains within my application. The issue is that when I run my Cucumber tests, I receive the following error when my application performs a redirect_to which contains a subdomain:
features/step_definitions/web_steps.rb:27
the scheme http does not accept registry part: test_url.example.com (or bad hostname?)
I have a signup controller action that creates the user and the account chosen and redirects the the user to the logout method with the subdomain specified based on what the user selected as the subdomain in the sign up form. Here is the code for the redirect action that happens once the user and account models are created and saved:
redirect_to :controller => "sessions", :action => "destroy", :subdomain => @account.site_address
Here are my rails 3 routes:
constraints(Subdomain) do
resources :sessions
match 'login', :to => 'sessions#new', :as => :login
match 'logout', :to => 'sessions#destroy', :as => :logout
match '/' => 'accounts#show'
end
Here is the code I have so far for the Subdomain class which is specified in the constraint above:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
I added UrlHelper to the ApplicationController:
class ApplicationController < ActionController::Base
include UrlHelper
protect_from_forgery
end
This is the code for the above UrlHelper class:
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
All of this code above allows me to run subdomains fine within the local browser. The issue above happens when I run my Cucumber test. The test clicks the signup button which in turn calls the redirect_to and throws the exception listed above.
Here is what my gem file looks like:
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
Could you please let be know an additional method to have my tests work now? I would be interested in either a fix or a way I can test my methods without using subdomains (for example a mocked out method that retrieves the account name).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的代码中有同样的模式。我使用水豚(但不使用黄瓜),并且我能够像这样绕过它:
主机!命令有效地更改了测试请求中向应用程序显示的主机。
编辑:刚刚意识到这适用于 webrat,但不适用于 capybara(我正在使用两者,现在正在逐步淘汰 webrat。)我在 capybara 中执行此操作的方式是单击指向新域的链接(capybara 跟随它)或:
编辑:另一个更新。找到了一种无需在每个事件中使用完整 URL 即可工作的方法。
在测试设置中。
I have this same pattern in my code. I use Capybara (but not Cucumber), and I was able to get around it like this:
The host! command effectively changes the host as it appears to the app from the test request.
EDIT: Just realized this was working with webrat, but not capybara (i'm using both, phasing out webrat now.) The way I'm doing it in capybara is to either click a link to a new domain (capybara follows it) or to:
EDIT: Another update. Found a method that works without having to use the full URL in every event.
In the test setup.