Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unfortunately you cannot use subdomain in capybara's test but I have a workaround for this problem. I have helper class for resolving a subdomain from the request, see:</p> <pre><code>class SubdomainResolver class &lt;&lt; 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 </code></pre> <p>As you see, when the app is running in <code>test</code> mode and a special <code>_subdomain</code> request param is set the subdomain is taken from the request param called <code>_subdomain</code> otherwise <code>request.subdomain</code> (normal subdomain) is used.</p> <p>To make this workaround work you have to also override url builders, in <code>app/helpers</code> create the following module:</p> <pre><code>module UrlHelper def url_for(options = nil) if cannot_use_subdomain? if options.kind_of?(Hash) &amp;&amp; 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 </code></pre> <p><code>SubdomainResolver.current_subdomain_from</code> can be also used as constraint in <code>config/routes.rb</code></p> <p>I hope it will help you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload