Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3: How do I visit a subdomain in a steak(rspec) spec using Capybara

I want to access user1.application.local.dev/panel/new from a steak spec.

How do I do it?

like image 720
Nerian Avatar asked Dec 19 '10 18:12

Nerian


2 Answers

Step 1. Set up a local DNS.

http://intridea.com/2010/6/2/using-bind-locally-on-os-x-for-easy-access-to-subdomains?blog=company

Step 2. Use a Capybara driver that support subdomains.

Either Selenium or Akephalos would do the trick.

Create spec/support/custom_env and put this content in it:

#Capybara.default_driver = :selenium
Capybara.default_driver = :akephalos
Capybara.app_host = 'http://davinci.testing.dev:8082'
Capybara.run_server = false
Capybara.server_port = 8082

Select the capybara driver that you want, either Selenium or akpehalos or whatever you want, except rack-test (default)

Put the domain and port of you choice, of course.

Step 3:

Add the config.before block to your spec/spec_helper.rb

RSpec.configure do |config|
config.before :each do Capybara.app_host = "http://davinci.testing.dev:8082" end
end

Put the domain and port of you choice, of course.

Step 4:

Add a helper to switch subdomains.

Spec/acceptance/support/helpers.rb

def switch_to_subdomain(subdomain)
   Capybara.app_host = "http://#{subdomain}.davinci.testing.dev:8082"
end

Put the domain and port of you choice, of course.

Step 5. Use the helper method in your spec.

Now every-time you want to change of subdomain you do:

scenario "Show school" do                        
   school = School.make!(:name=>"perico")
   switch_to_subdomain(school.name)    
   visit("/")                      
   page.has_content?("Welcome to perico")
end
like image 52
Nerian Avatar answered Nov 19 '22 18:11

Nerian


This is a Capybara question. Set the default_host when you need it

Capybara.default_host = 'sub.domain.com' 
like image 27
Ariejan Avatar answered Nov 19 '22 19:11

Ariejan