5

私は現在、レールテストへの長い旅の終わりに近づいていますが、サブドメインで動作するリクエストスペックを取得する方法について頭を悩ませています.

開発では、次のような URL で pow を使用しています。http://teddanson.myapp.dev/accountこれはすべて問題なくダンディです。

http://127.0.0.1:50568/accountテストでは、サブドメイン全体とは明らかにうまくいかないlocalhostを返すことをカピバラにさせました。サブドメインを必要としないアプリのパブリック部分では問題なく動作しますが、特定のユーザーのサブドメイン アカウントにアクセスする方法は私には理解できません。

関連するルートには、次の方法でアクセスできます。

class Public
  def self.matches?(request)
    request.subdomain.blank? || request.subdomain == 'www'
  end
end

class Accounts
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != 'www'
  end
end

私は狂った薬を服用しているように感じるので、誰かが私を助けるためのアドバイスや提案があれば、それは非常に素晴らしいことです. ご協力いただきありがとうございます!

4

2 に答える 2

2

https://web.archive.org/web/20171222062651/http://chrisaitchison.com/2013/03/17/testing-subdomains-で説明されているように、xip.io を使用して Capybara/RSpec でサブドメインをテストできます。インレール/

于 2013-03-17T21:47:25.870 に答える
1

残念ながら、カピバラのテストではサブドメインを使用できませんが、この問題の回避策があります。リクエストからサブドメインを解決するためのヘルパー クラスがあります。以下を参照してください。

class SubdomainResolver
  class << self
    # Returns the current subdomain
    def current_subdomain_from(request)
      if Rails.env.test? and request.params[:_subdomain].present?
        request.params[:_subdomain]
      else
        request.subdomain
      end
    end
  end
end

ご覧のとおり、アプリがtestモードで実行され、特別な_subdomainリクエスト パラメータが設定されている場合、サブドメインはリクエスト パラメータから取得されます。それ以外の_subdomain場合request.subdomain(通常のサブドメイン) が使用されます。

この回避策を機能させるにはapp/helpers、次のモジュールを作成して、 URL ビルダーもオーバーライドする必要があります。

module UrlHelper
  def url_for(options = nil)
    if cannot_use_subdomain?
      if options.kind_of?(Hash) && options.has_key?(:subdomain)
        options[:_subdomain] = options[:subdomain]
      end
    end

    super(options)
  end

  # Simple workaround for integration tests.
  # On test environment (host: 127.0.0.1) store current subdomain in the request param :_subdomain.
  def default_url_options(options = {})
    if cannot_use_subdomain?
      { _subdomain: current_subdomain }
    else
      {}
    end
  end

  private

  # Returns true when subdomains cannot be used.
  # For example when the application is running in selenium/webkit test mode.
  def cannot_use_subdomain?
    (Rails.env.test? or Rails.env.development?) and request.host ==  '127.0.0.1'
  end
end

SubdomainResolver.current_subdomain_fromの制約としても使用できますconfig/routes.rb

お役に立てば幸いです。

于 2012-08-31T09:04:44.717 に答える