-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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.
thank you so much, you saved my life.
@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
@yazinsai standard is to keep them under
spec/support
and use thisDir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
in
rails_helper.rb
to require the files :)