I want to test my multidomain RoR3 App.
Here's my test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'capybara/rails'
require 'blueprints'
class ActiveSupport::TestCase
end
class ActionDispatch::IntegrationTest
include Capybara
def host
"http://#{subdomain}.lvh.me:3000"
end
def subdomain
@subdomain ? @subdomain : 'demostore'
end
def visit(url)
super("http://#{subdomain}.lvh.me:3000#{url}")
end
end
And my integration test:
require 'test_helper'
class ProductsTest < ActionDispatch::IntegrationTest
def setup
@subdomain = 'demostore'
# creating stuff
end
def teardown
# deleting stuff
end
test "user views product list" do
visit('/')
assert page.has_css?('ul.product-listing')
assert page.has_xpath?("//ul[@class='product-listing']/li", :count => 12)
end
test "user views product page" do
product = Product.first
visit('/')
find(:xpath, "//ul[@class='product-listing']/li/a[1]").click
save_and_open_page
end
end
And I'm sure the link exists. There is problem with clicking and filling stuff.
click_link('Existent link title')
doesn't work too.
I think the default Capybara's driver Rack::Test could have problems with this multidomain stuff?
In your setup, call this rack::test function, which will change your host's value. Well, it changes the host that gets returned about the fake web request.
host! "#{store.subdomain}.example.com"
The problem was that i'm using multidomain stuff so I had to use lvh.me which resolves localhost. You can do the same think by setting in Your /etc/hosts
127.0.0.1 subdomain.yourapp.local
and then use this domain.
I've overwritten Capybara's visit method with sth like that:
def visit(link)
super("mysubdomain.lvh.me:3000#{link}")
end
but problem persisted because when Capybara clicked for example link, the visit method was not used and my host was not requested. Which was? I don't know - probably the default one.
So solution is to set host and port in Capybara settings:
class ActionDispatch::IntegrationTest
include Capybara
Capybara.default_host = "subdomain.yourapp.local"
Capybara.server_port = 3000
# ... rest of stuff here
end
Apparently it's a problem with rack-test.
But there is a fork of it by hassox that just solved it for me. It's just a couple of commits that really matter, in case you want to check what the changes are.
This is how my Gemfile looks:
group :test, :cucumber do
gem 'rack-test', :git => "https://github.com/hassox/rack-test.git"
gem 'capybara', '= 0.4.1.2'
gem 'capybara-envjs', '= 0.4.0'
gem 'cucumber-rails', '>= 0.3.2'
gem 'pickle', '>= 0.3.4'
end
And then I just make sure to
visit('http://my_subdomain.example.com')
in my steps. Now I'm trying to understand what would make url helpers work with subdomains.
Here's a quick setup that may help you out...
rails 3.2+ testing custom subdomains using cucumber capybara with pow setup:
https://gist.github.com/4465773
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With