Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

ruby on rails - Capybara with subdomains - default_host

I have an app that uses subdomains to switch databases (multi-tenancy). I'm trying to use Capybara for integration testing, and it really relies a lot on subdomains.

My understanding was that setting Capybara.default_host= to something would make all my requests come from this host. This doesn't seem to be the case. In this post, the author recommends just visiting the explicit url with a host, but this becomes a bit annoying if I'm navigating all over the place. I'd like to just set the host, then be able to use my rails paths as expected. Not sure what I'm doing wrong, but here's what I've tried:

# spec_helper.rb
RSpec.configure do |config|
  config.before(:each, :type => :request) do
    Capybara.default_host = 'http://app.mydomain.com'
  end
end

# in some_integration_spec.rb
before do
  puts "Capybara.default_host: #{Capybara.default_host}"
  puts "some_app_url: #{some_app_url}"
end

This yields the output:

Capybara.default_host: http://app.mydomain.com
some_app_url: http://www.example.com/some_path

What am I doing wrong? default_host appears to do nothing. As I say, I don't want to have to say visit(Capybara.default_host + some_app_path) as that's a bit annoying each time. Why else does this default_host option exist?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not sure of the intended use of default_host, but app_host does what you need. I've found I first need to call the rails session method host! in order to set the host string that will be passed to controllers in the request object.

Then you need to set Capybara.app_host to tell Capybara to call your app via the web server instead of just making the calls in process. If you don't do that then Capybara wigs out when it encounters redirects and drops the host information in the second request.

I'm not sure why this doesn't take care of the Rails request end of things automatically, but I've found that unless I set the host in both places explicitly, then I get inconsistent results.

def set_host (host)
  host! host
  Capybara.app_host = "http://" + host
end

before(:each) do
  set_host "lvh.me:3000"
end

Then you can just use relative paths to access pages.

Update:

Capybara 2.x and rspec-rails 2.12.0 introduced "Feature" specs for running Capybara acceptance tests. The new FeatureExampleGroup module in rspec-rails is different from RequestExampleGroup and no longer has access to the rack-test host! method. Now you want to use default_url_options instead:

def set_host (host)
  # host! host
  default_url_options[:host] = host
  Capybara.app_host = "http://" + host
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...