Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing multidomain Rails 3 app with Capybara

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?

like image 869
dreake Avatar asked Jan 18 '11 12:01

dreake


4 Answers

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"
like image 86
Jesse Wolgamott Avatar answered Nov 19 '22 19:11

Jesse Wolgamott


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
like image 25
dreake Avatar answered Nov 19 '22 20:11

dreake


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.

like image 1
ilpoldo Avatar answered Nov 19 '22 20:11

ilpoldo


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

like image 1
SVR Avatar answered Nov 19 '22 20:11

SVR