带有子域的水豚-default_host [英] Capybara with subdomains - default_host

查看:88
本文介绍了带有子域的水豚-default_host的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用子域来切换数据库(多租户)的应用。我正在尝试使用Capybara进行集成测试,它确实非常依赖子域。

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.

我的理解是设置 Capybara.default_host = 会使我的所有请求都来自此主机。似乎并非如此。在这篇文章中,作者建议只使用主机访问显式URL,但这如果我到处导航,会有点烦人。我只想设置主机,然后能够按预期使用我的rails路径。不知道我在做什么错,但是这是我尝试过的事情:

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

这将产生输出:

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

我在做什么错? default_host 似乎不起作用。就像我说的,我不想说 visit(Capybara.default_host + some_app_path),因为每次都有点烦人。为什么还存在default_host选项?

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?

推荐答案

我不确定 default_host的预期用途,但是 app_host 可以满足您的需求。我发现我首先需要调用rails会话方法 host!来设置将传递给请求对象中的控制器的主机字符串。

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.

然后,您需要设置 Capybara.app_host 告诉Capybara通过网络服务器调用您的应用,而不仅仅是进行通话。如果您不这样做,则Capybara在遇到重定向时会掉线,并在第二个请求中删除主机信息。

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.

我不确定为什么不这样做自动处理Rails的 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.

更新:

Capybara 2.x rspec-rails 2.12.0 引入了用于运行Capybara验收测试的功能规范。 rspec-rails 中新的 FeatureExampleGroup 模块与 RequestExampleGroup ,并且不再有权使用机架测试主机!方法。现在您要使用 default_url_options 代替:

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

这篇关于带有子域的水豚-default_host的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆