Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Oct 12, 2016

Thanks, your code saved me!!

@dskecse

This comment has been minimized.

Copy link

commented Jun 19, 2017

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

@Xosmond

This comment has been minimized.

Copy link

commented Nov 22, 2017

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

@Xosmond

This comment has been minimized.

Copy link

commented Nov 22, 2017

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

Why?

@ianrandmckenzie

This comment has been minimized.

Copy link

commented Aug 24, 2018

This is brilliant, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.