9

マルチドメイン RoR3 アプリをテストしたいと考えています。

ここに私のtest_helper.rbがあります

ENV["RAILS_ENV"] = "test"

require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'capybara/rails'
require 'blueprints'

class ActiveSupport::TestCase

end

class ActionDispatch::IntegrationTest
  include Capybara

  def host
    "http://#{subdomain}.lvh.me:3000"
  end

  def subdomain
    @subdomain ? @subdomain : 'demostore'
  end

  def visit(url)
    super("http://#{subdomain}.lvh.me:3000#{url}")
  end
end

そして私の統合テスト:

require 'test_helper'

class ProductsTest < ActionDispatch::IntegrationTest

  def setup
    @subdomain = 'demostore'
    # creating stuff
  end

  def teardown
    # deleting stuff
  end

  test "user views product list" do
    visit('/')
    assert page.has_css?('ul.product-listing')
    assert page.has_xpath?("//ul[@class='product-listing']/li", :count => 12)
  end

  test "user views product page" do
    product = Product.first

    visit('/')
    find(:xpath, "//ul[@class='product-listing']/li/a[1]").click
    save_and_open_page
  end

end

そして、リンクが存在すると確信しています。クリックして入力する際に​​問題があります。

click_link('Existent link title')

も機能しません。

デフォルトの Capybara のドライバー Rack::Test は、このマルチドメインに問題があると思いますか?

4

6 に答える 6

1

セットアップで、このrack :: test関数を呼び出します。これにより、ホストの値が変更されます。まあ、それは偽のウェブリクエストについて返されるホストを変更します。

host! "#{store.subdomain}.example.com"
于 2011-01-18T20:13:40.070 に答える
1

ここにあなたを助けるかもしれない簡単なセットアップがあります...

Rails 3.2+ キュウリ カピバラを pow セットアップで使用してカスタム サブドメインをテストする:

https://gist.github.com/4465773

于 2013-01-06T07:19:31.457 に答える
1

問題は、マルチドメインのものを使用しているため、localhost を解決する lvh.me を使用する必要があったことです。/etc/hosts に設定することで、同じことを考えることができます

127.0.0.1 subdomain.yourapp.local

そして、このドメインを使用します。

次のように、カピバラの visit メソッドを sth で上書きしました。

def visit(link)
  super("mysubdomain.lvh.me:3000#{link}")
end

しかし、カピバラが例のリンクをクリックしたときに、訪問方法が使用されず、ホストが要求されなかったため、問題は解決しませんでした。どれが?わかりません - おそらくデフォルトのものです。

したがって、解決策は、Capybara 設定でホストとポートを設定することです。

class ActionDispatch::IntegrationTest
  include Capybara

  Capybara.default_host = "subdomain.yourapp.local"
  Capybara.server_port = 3000
  # ... rest of stuff here
end
于 2011-01-25T09:08:00.163 に答える
1

この問題の優れた解決策であることがわかったものを共有したいと思います。URL の先頭に必要なサブドメインを追加するヘル​​パー メソッドを作成し、Capybara メソッドを上書きせず、Rack::Test および capybara-webkit ドライバーで動作します。実際、カピバラすら使わないスペックでも動きます。(ソース: http://minimul.com/capybara-and-subdomains.html )

Spec ヘルパー メソッド

# spec/support/misc.helpers.rb
def hosted_domain(options = {})
  path = options[:path] || "/" # use root path by default
  subdomain = options[:subdomain] || 'www'
  if example.metadata[:js]
    port = Capybara.current_session.driver.server_port
    url = "http://#{ subdomain }.lvh.me:#{ port }#{ path }"
  else
    url = "http://#{ subdomain }.example.com#{ path }"
  end
end


その使用法を説明するために、2 つの例を次に示します。

Feature Spec で使用 (Capybara を使用)

require 'spec_helper'

describe "Accounts" do
  # Creates an account using a factory which sequences
  # account subdomain names
  # Additionally creates users associated with the account
  # using FactoryGirl's after callbacks (see FactoryGir docs)
  let (:account) { FactoryGirl.create(:account_with_users) })

  it "allows users to sign in" do
    visit hosted_domain(path: new_sessions_path, subdomain: account.subdomain)

    user = account.users.first

    fill_in "email", with: user.email
    fill_in "password", with: user.password
    click_button "commit"

    # ... the rest of your specs
  end
end

Request Spec で使用 (Capybara なし)

#spec/requests/account_management_spec.rb
require "spec_helper"

describe "Account management" do
  # creates an account using a factory which sequences
  # account subdomain names
  let (:account) { FactoryGirl.create(:account) })

  it "shows the login page" do
    get hosted_domain(path: "/login", subdomain: account.subdomain)
    expect(response).to render_template("sessions/new")
  end

end
于 2013-05-07T20:27:50.433 に答える
1

どうやらラックテストの問題です。

しかし、私のためにそれを解決したhassoxによるフォークがあります。変更内容を確認したい場合に備えて、本当に重要なのはほんの数回のコミットです。

これは私のGemfileがどのように見えるかです:

group :test, :cucumber do
  gem 'rack-test', :git => "https://github.com/hassox/rack-test.git"
  gem 'capybara', '= 0.4.1.2'
  gem 'capybara-envjs', '= 0.4.0'
  gem 'cucumber-rails', '>= 0.3.2'
  gem 'pickle', '>= 0.3.4'
end

そして、私はちょうど

visit('http://my_subdomain.example.com')

私のステップで。現在、URLヘルパーがサブドメインで機能する理由を理解しようとしています。

于 2011-02-16T18:17:17.103 に答える
0

シンプルでクリーンな解決策は、Capybara の visit メソッドに提供する URL をオーバーライドすることです。*.lvh.me ドメインでうまく機能し、localhost にリダイレクトされます。

describe "Something" do

  def with_subdomain(link)
    "http://subdomain.lvh.me:3000#{link}"
  end

  it "should do something" do
    visit with_subdomain(some_path)
  end

end

または、spec の前に app_host を再定義して、同じことを行うこともできます。

Capybara.app_host = 'http://sudbomain.lvh.me:3000'
..
visit(some_path)
于 2012-10-08T05:46:47.667 に答える