14

Rails 3、キュウリ 0.9.4、カピバラ 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! 信じられないことですが、本当です。私は現在のURLをログに記録しましたが、それは1つのシナリオのためsubdomain.example.comcucumber features/subdomain.featureものwww.example.comですcucumber features

Scenario: subdomain scenario
  Given I visit subdomain "subdomain"

両方の場合において!

理由はわかりません...

カピバラでサブドメインをテストする最良の方法はありますか?

4

3 に答える 3

13

さて、これは、サブドメインを切り替えるたびに新しいセッションを作成できるという、望ましい動作を実現する、非常に単純で理解しやすい Capybara のハックです。これは、ユーザーが 1 つのドメインに登録し (その結果、アカウント用にサブドメインが作成される)、そのサブドメインに移動する必要があるサイトで役立ちます。

まず第一に (そして、この部分は他のソリューションにかなり共通しています)、先に進み、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

次に、これを機能させる魔法のモンキーパッチについて説明します。基本的に、サブドメインがいつ変更されたかを検出し、RackTest セッション オブジェクトをリセットできるように、Capybara を賢くする必要があります。

# 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 で動作しますが、修正しない限り別のバージョンでは動作しない可能性があります。幸運を。

于 2011-02-21T06:05:54.920 に答える
5

あなたのニーズにセッションとは何の関係も含まれておらず、あとは別のサブドメインにアクセスすることだけである場合、私は次の関数を書きました:

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
于 2013-03-29T20:53:49.130 に答える
1

少しの間同じ問題があり、私のテストでは、サブドメイン間で切り替えたりリダイレクトしたりする必要がありました。

このステップを考えると:

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

When I go to "http://mysubdomain.example.org"ラック テストでは機能しますが、アプリによってリダイレクトされるか、他のパスへのリンクをたどると、ホストは default_host に戻ります。

ラックテストが前のリクエストで使用されたホストを追跡することを保証する hassox によるラックテストのフォークがあります。

したがって、私のGemfileでは、カピバラを要求する前に:

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

特定のサブドメインにアクセスする必要がある場合 (または、アプリがsecure.myapp.com/sign_inなどのサブドメインにリダイレクトする場合)、この機能はブラウザーの動作のように読み取ることができると確信しています:カピバラを使用するかvisit("somesubdomain.example.org")、アプリによって別のホストにリダイレクトされるまで、現在のサブドメイン。

于 2011-04-10T08:10:19.377 に答える