I know this is a relatively old question, but I found that it depends on which test you are using. I also run Rails 4 and RSpec 3.2, so I'm sure some things have changed since this question was asked.
Query Specifications
before { host! "#{mock_subdomain}.example.com" }
Feature Features with Capybara
before { Capybara.default_host = "http://#{mock_subdomain}.example.com" } after { Capybara.default_host = "http://www.example.com" }
I usually create modules in spec/support that look something like this:
# spec/support/feature_subdomain_helpers.rb module FeatureSubdomainHelpers # Sets Capybara to use a given subdomain. def within_subdomain(subdomain) before { Capybara.default_host = "http://#{subdomain}.example.com" } after { Capybara.default_host = "http://www.example.com" } yield end end # spec/support/request_subdomain_helpers.rb module RequestSubdomainHelpers # Sets host to use a given subdomain. def within_subdomain(subdomain) before { host! "#{subdomain}.example.com" } after { host! "www.example.com" } yield end end
Include in spec/rails_helper.rb :
RSpec.configure do |config|
Then you can call in your specification like this:
feature 'Admin signs in' do given!(:admin) { FactoryGirl.create(:user, :admin) } within_subdomain :admin do scenario 'with valid credentials' do
Chris Peters May 23 '15 at 13:55 2015-05-23 13:55
source share