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

显然这是机架测试的问题。

但是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 回答
1

问题是我正在使用多域的东西,所以我不得不使用解析本地主机的 lvh.me。您可以通过设置您的 /etc/hosts 来做同样的事情

127.0.0.1 subdomain.yourapp.local

然后使用这个域。

我已经用这样的东西覆盖了 Capybara 的访问方法:

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

但是问题仍然存在,因为当 Capybara 单击示例链接时,未使用访问方法并且未请求我的主机。哪个是?我不知道——可能是默认的。

所以解决方案是在 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

在你的设置中,调用这个 rack::test 函数,它会改变你主机的值。好吧,它会更改返回的有关虚假 Web 请求的主机。

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

我想分享一下我发现解决这个问题的好方法。它涉及创建一个辅助方法以在 URL 前添加所需的子域,不覆盖任何 Capybara 方法,并与 Rack::Test 和 capybara-webkit 驱动程序一起使用。事实上,它甚至可以在甚至不使用 Capybara 的规格中工作。(来源: http: //minimul.com/capybara-and-subdomains.html

规范助手方法

# 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


为了说明它的用途,这里有两个例子:

用于功能规范(使用 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

在请求规范中使用(没有 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 回答
0

一个简单而干净的解决方案是覆盖您提供给 Capybara 访问方法的 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

或者你可以通过在规范之前重新定义 app_host 来做同样的事情:

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