Rspec rails install subdomain

I am using rSpec to test my application. In my application controller, I have a way like this:

def set_current_account @current_account ||= Account.find_by_subdomain(request.subdomains.first) end 

Is it possible to set request.subdomain in my specification? Maybe in the previous block? I am new to rSpec, so any advice on this would be greatly appreciated.

Eef

+50
ruby-on-rails subdomain specifications rspec
Mar 31 '10 at 21:32
source share
4 answers

I figured out how to sort this problem.

In my previous block in my specs, I simply added:

 before(:each) do @request.host = "#{mock_subdomain}.example.com" end 

This sets the request.subdomains.first value to mock_subdomain.

Hope someone finds this helpful, as it was not well explained elsewhere on the net.

+81
Apr 01 2018-10-12T00:
source share
β€” -

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| # ... # Extensions config.extend FeatureSubdomainHelpers, type: :feature config.extend RequestSubdomainHelpers, type: :request end 

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 # ... end scenario 'with invalid password' do # ... end end end 
+25
May 23 '15 at 13:55
source share

In rails 3, everything that I tried to manually install on the host did not work, but looking at the code, I noticed how beautifully they analyzed the path that you pass to the request helpers, for example get . Of course, if your controller goes and selects the user mentioned in the subdomain, and saves it as @king_of_the_castle

 it "fetches the user of the subomain" do get "http://#{mock_subdomain}.example.com/rest_of_the_path" assigns[:king_of_the_castle].should eql(User.find_by_name mock_subdomain) end 
+8
Dec 01 '10 at 1:29
source share
  • RSpec - 3.6.0
  • Capybara - 2.15.1

Chris Peters answer worked for me for Request specifications, but for Feature specifications I had to make the following changes:

rails_helper:

 Capybara.app_host = 'http://lvh.me' Capybara.always_include_port = true 

feature_subdomain_helpers:

 module FeatureSubdomainHelpers def within_subdomain(subdomain) before { Capybara.app_host = "http://#{subdomain}.lvh.me" } after { Capybara.app_host = "http://lvh.me" } yield end end 
0
Dec 01 '17 at 7:15
source share



All Articles