pet*_*hka 14 subdomain integration-testing cucumber capybara ruby-on-rails-3
Rails 3,Cucumber 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
Run Code Online (Sandbox Code Playgroud)
它运行,如果我运行,cucumber features/subdomain.feature但如果我运行它失败cucumber features!这是令人难以置信的,但这是真的.我记录当前的URL,这是subdomain.example.com对cucumber features/subdomain.feature和www.example.com对cucumber features与一个场景
Scenario: subdomain scenario
Given I visit subdomain "subdomain"
Run Code Online (Sandbox Code Playgroud)
在这两种情况下!
我不知道原因......
有没有最好的方法来测试带有水豚的子域名?
Obi*_*bie 13
好的,这里应该是一个相当直接且容易理解的Capybara黑客产生所需的行为,即每次切换子域时能够创建一个新的会话.这对于用户在一个域上注册(导致为其帐户创建子域)的站点非常有用,然后最终需要导航到该子域.
首先(这部分与其他解决方案相当普遍)继续给自己一个方法来改变Cucumber步骤中的Capybara.default_host.我是这样做的:
Then /^I switch the subdomain to (\w+)$/ do |s|
Capybara.default_host = "#{s}.smackaho.st"
end
Run Code Online (Sandbox Code Playgroud)
在您希望使用新子域的位置将此步骤粘贴到Cucumber功能中.例如:
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
Run Code Online (Sandbox Code Playgroud)
现在进行神奇的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
Run Code Online (Sandbox Code Playgroud)
此补丁适用于Capybara 0.4.1.1,除非经过修改,否则可能不适用于不同的版本.祝好运.
如果您的需求不包含任何与会话有关的内容,并且您所访问的是访问不同的子域,那么我写了这个函数:
def visit_with_subdomain(uri, options = {})
domain = Capybara.default_host
port = Capybara.server_port
subdomain = options[:subdomain]
visit "http://#{subdomain}.#{domain}:#{port}#{uri}"
end
Run Code Online (Sandbox Code Playgroud)
你可以这样称呼它:
visit_with_subdomain some_path, subdomain: some_subdomain
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5309 次 |
| 最近记录: |