Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rtekie
Created May 14, 2012 13:14
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save rtekie/2693866 to your computer and use it in GitHub Desktop.
Save rtekie/2693866 to your computer and use it in GitHub Desktop.
Support for Rspec / Capybara subdomain integration testing
# Support for Rspec / Capybara subdomain integration testing
# Make sure this file is required by spec_helper.rb
#
# Sample subdomain test:
# it "should test subdomain" do
# switch_to_subdomain("mysubdomain")
# visit root_path
# end
DEFAULT_HOST = "lvh.me"
DEFAULT_PORT = 9887
RSpec.configure do |config|
Capybara.default_host = "http://#{DEFAULT_HOST}"
Capybara.server_port = DEFAULT_PORT
Capybara.app_host = "http://#{DEFAULT_HOST}:#{Capybara.server_port}"
end
def switch_to_subdomain(subdomain)
Capybara.app_host = "http://#{subdomain}.#{DEFAULT_HOST}:#{DEFAULT_PORT}"
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
@turadg
Copy link

turadg commented Apr 16, 2013

Great idea! 👍

After some testing, I found that the port wasn't necessary if you use Capybara's always_include_port option. Here's a version that does that instead, and has a method to switch back to the main domain (which is now necessary to specify at the top of a non-subdomain spec since it will run with others in a suite):

https://gist.github.com/turadg/5399790

@som4ik
Copy link

som4ik commented Oct 12, 2016

Thanks, your code saved me!!

@dskecse
Copy link

dskecse commented Jun 19, 2017

@rtekie why would one need to wrap the setters with RSpec.configure here?

@Xosmond
Copy link

Xosmond commented Nov 22, 2017

For me it doesn't work, only if I change app_host each test, it works. :(

@Xosmond
Copy link

Xosmond commented Nov 22, 2017

On before suite -> does not work
On before all -> does not work
on before each -> works

Why?

@ianrandmckenzie
Copy link

This is brilliant, thanks for sharing!

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