Rails 3,黄瓜0.9.4,Capybara 0.4.0
Given /^I visit subdomain "(.+)"$/ do |sub| Capybara.default_host = "#{sub}.example.com" #for Rack::Test Capybara.app_host = "http://#{sub}.example.com:9887" if Capybara.current_driver == :culerity end
如果我运行黄瓜功能/ subdomain.feature它工作,但如果我运行黄瓜功能失败!这是令人难以置信的,但它是真实的.我记录了当前的网址,它是subdomain.example.com的黄瓜功能/ subdomain.feature和www.example.com黄瓜功能一个场景与
Scenario: subdomain scenario Given I visit subdomain "subdomain"
在这两种情况下!
我不知道原因…
有没有最好的方法来测试subdomains与水豚?
解决方法
好的,这里应该是一个相当直截了当且容易理解的黑手起家Capybara,产生所需的行为,即能够在每次切换子域时创建一个新的会话.这对于用户在一个域中注册的站点(其导致为其帐户创建的子域),然后最终需要导航到该子域是有用的.
首先(这部分在其他解决方案中相当普遍),然后给自己一个方法来改变黄瓜级别的Capybara.default_host.我这样做:
Then /^I switch the subdomain to (\w+)$/ do |s| Capybara.default_host = "#{s}.smackaho.st" end
在您要使用新子域的时候,将此步骤保存在黄瓜功能中.例如:
When I open the email Then I should see "http://acme.rightbonus.com/users/confirmation" in the email body Given I switch the subdomain to acme When I follow "Click here to finish setting up your account" in the email Then I should be on the user confirmation page for acme
现在为了使这项工作的神奇monkeypatching.基本上,您希望Capybara足够聪明,以检测子域是否已更改并重置其RackTest会话对象.
# features/support/capybara.rb class Capybara::Driver::RackTest # keep track of the default host you started with def initialize(app) raise ArgumentError,"rack-test requires a rack application,but none was given" unless app @app = app @default_host = Capybara.default_host end def process(method,path,attributes = {}) reset_if_host_has_changed path = ["http://",@default_host,path].join return if path.gsub(/^#{request_path}/,'') =~ /^#/ path = request_path + path if path =~ /^\?/ send(method,to_binary(path),to_binary( attributes ),env) follow_redirects! end private def build_rack_mock_session # :nodoc: puts "building a new Rack::MockSession for " + Capybara.default_host Rack::MockSession.new(app,Capybara.default_host || "www.example.com") end def reset_if_host_has_changed if @default_host != Capybara.default_host reset! # clears the existing MockSession @default_host = Capybara.default_host end end end
此补丁适用于Capybara 0.4.1.1,除非经过修改,否则可能无法使用不同的版本.祝你好运.