【问题标题】:Rails, Capybara and subdomains: how to visit certain subdomainRails、Capybara 和子域:如何访问某些子域
【发布时间】:2010-11-18 17:51:39
【问题描述】:

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

如果我运行cucumber features/subdomain.feature,它可以工作,但如果我运行cucumber features,它会失败!这令人难以置信,但这是真的。我记录了当前的网址,它是subdomain.example.com 用于cucumber features/subdomain.featurewww.example.com 用于cucumber features 的一种情况与

Scenario: subdomain scenario
  Given I visit subdomain "subdomain"

在这两种情况下!

不知道是什么原因……

有没有用 capybara 测试子域的最佳方法?

【问题讨论】:

    标签: ruby-on-rails-3 cucumber integration-testing capybara subdomain


    【解决方案1】:

    好的,这应该是一个相当简单易懂的 Capybara hack,它产生了所需的行为,即每次切换子域时都能够创建一个新会话。这对于用户在一个域上注册(这会导致为其帐户创建一个子域)然后最终需要导航到该子域的站点很有用。

    首先(这部分对于其他解决方案来说是相当常见的)继续并给自己一个在 Cucumber 步骤中更改 Capybara.default_host 的方法。我是这样做的:

    Then /^I switch the subdomain to (\w+)$/ do |s|
      Capybara.default_host = "#{s}.smackaho.st"
    end
    

    将此步骤粘贴到您希望使用新子域的 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
    

    现在是让这项工作发挥作用的神奇猴子补丁。基本上,您希望 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,除非经过修改,否则可能不适用于不同的版本。祝你好运。

    【讨论】:

    • 是的!有趣的解决方案。但它有一个问题。如果我使用“显示页面”,我得到:预期:“/admin/companies”,得到:“/home/user/webdev/project/public/admin/companies”(使用 ==)(RSpec::Expectations ::ExpectationNotMetError)
    • 那么步骤“那么我应该在 acme 的用户确认页面上”的描述是什么?
    【解决方案2】:

    如果您的需求不包括与会话有关的任何内容,并且您所追求的只是访问不同的子域,那么我编写了这个函数:

    def visit_with_subdomain(uri, options = {})
      domain = Capybara.default_host
      port = Capybara.server_port
      subdomain = options[:subdomain]
      visit "http://#{subdomain}.#{domain}:#{port}#{uri}"
    end
    

    你可以这样称呼它:

    visit_with_subdomain some_path, subdomain: some_subdomain
    

    【讨论】:

      【解决方案3】:

      遇到了同样的问题,我的测试有时不得不在子域之间来回切换或重定向。

      鉴于这一步:

      When /^(?:|I )go to "(.+)"$/ do |url|
        visit url
      end
      

      When I go to "http://mysubdomain.example.org" 在机架测试中工作,但如果您被应用重定向或点击指向其他路径的链接,主机将恢复为 default_host。

      hassox 有一个fork of rack-test,可确保 rack-test 跟踪上一个请求中使用的主机。

      所以,在我的 Gemfile 中,在需要 capybara 之前:

        gem 'rack-test', :git => "https://github.com/hassox/rack-test.git"
      

      现在,如果我需要访问特定的子域(或者应用程序将我重定向到一个子域,例如 secure.myapp.com/sign_in),我确信该功能可以更像浏览器一样阅读会表现:它将允许我留在当前子域上,直到我去——使用水豚的visit("somesubdomain.example.org")——或者被应用程序重定向到不同的主机。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-20
        • 2015-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多