Skip to content

Instantly share code, notes, and snippets.

@turadg
Forked from rtekie/subdomains.rb
Last active January 21, 2024 19:45
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save turadg/5399790 to your computer and use it in GitHub Desktop.
Save turadg/5399790 to your computer and use it in GitHub Desktop.
Drop-in utility to test your app subdomains with Capybara, Rspec, and a Javascript driver (Poltergeist, Webkit, or Selenium)
# Support for Rspec / Capybara subdomain integration testing
# Make sure this file is required by spec_helper.rb
# (e.g. save as spec/support/subdomains.rb)
def switch_to_subdomain(subdomain)
# lvh.me always resolves to 127.0.0.1
hostname = subdomain ? "#{subdomain}.lvh.me" : "lvh.me"
Capybara.app_host = "http://#{hostname}"
end
def switch_to_main_domain
switch_to_subdomain nil
end
RSpec.configure do |config|
switch_to_main_domain
end
Capybara.configure do |config|
config.always_include_port = true
end
# Sample spec file
require 'spec_helper'
describe "Subdomains", js: true do
it "should test subdomain" do
switch_to_subdomain("mysubdomain")
visit root_path
end
end
describe "Main domain", js: true do
it "should test the primary domain" do
switch_to_main_domain
visit root_path
end
end
@yazinsai
Copy link

It'd be great if you could mention where these files should be placed in order for this to work. Thanks

@wcpaez
Copy link

wcpaez commented Dec 17, 2014

Very helpful, thanks a lot!

@gregblass
Copy link

This helped me tremendously. Thank you!

@steverob
Copy link

@yazinsai standard is to keep them under spec/support and use this
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
in rails_helper.rb to require the files :)

@shotop
Copy link

shotop commented Apr 2, 2015

This is great - any ideas how to do this without using a service url like lvh.me? We don't want to rely on an external dns resolution for our tests.

@yasalmasri
Copy link

thank you so much, you saved my life.

@dskecse
Copy link

dskecse commented Jun 19, 2017

@turadg how about replacing

RSpec.configure do |config|
  switch_to_main_domain
end

Capybara.configure do |config|
  config.always_include_port = true
end

with

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

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment