带有子域的水豚 - default_host
我有一个使用子域来切换数据库(多租户)的应用程序。我正在尝试使用 Capybara 进行集成测试,它确实非常依赖于子域。
我的理解是,将 Capybara.default_host= 设置为某项将使我的所有请求都来自该主机。事实似乎并非如此。在这篇文章中,作者建议仅使用主机访问显式网址,但是这如果我在各处导航,就会变得有点烦人。我想只设置主机,然后能够按预期使用我的 Rails 路径。不确定我做错了什么,但这是我尝试过的:
# 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 选项?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定
default_host
的预期用途,但app_host
可以满足您的需要。我发现我首先需要调用 Rails 会话方法host!
以便设置将传递给请求对象中的控制器的主机字符串。然后,您需要设置 Capybara.app_host 来告诉 Capybara 通过网络服务器调用您的应用程序,而不仅仅是在进程中进行调用。如果您不这样做,那么水豚在遇到重定向时就会出错,并在第二个请求中删除主机信息。
我不确定为什么这不会自动处理 Rails
request
的事情,但我发现除非我在两个地方显式设置主机,否则我会得到不一致的结果。然后就可以使用相对路径来访问页面了。
更新:
Capybara 2.x 和 rspec-rails 2.12.0 引入了用于运行 Capybara 验收测试的“功能”规范。
rspec-rails
中的新FeatureExampleGroup
模块与RequestExampleGroup
不同,并且不再有权访问机架测试主机!方法。现在您想使用
default_url_options
代替:I'm not sure of the intended use of
default_host
, butapp_host
does what you need. I've found I first need to call the rails session methodhost!
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.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 inrspec-rails
is different fromRequestExampleGroup
and no longer has access to the rack-testhost!
method. Now you want to usedefault_url_options
instead:当您需要更改 URL 以包含子域时,您可以在步骤定义中指定
app_host
。使用像lvh.me
这样的域名,因为它指向127.0.0.1
:Capybara 假定当您指定
app_host
时,您测试在端口 80 上运行的远程服务器,但在我们的例子中,我们正在测试在 Capybara 指定的随机端口上运行的本地应用程序。要解决此问题,请在您的env.rb
文件中添加以下行:现在,当您访问应用程序的页面时...
...该 url 将指定子域以及该子域所使用的端口。应用程序正在运行。
仅供参考:这对我使用 Capybara 2.0.2 有用。
When you need to change the URL to include the subdomain, you can specify the
app_host
in your step definitions. Use a domain likelvh.me
since it points to127.0.0.1
:Capybara assumes that when you're specifying an
app_host
that you're testing a remote server running on port 80, but in our case, we're testing a local app which is running on a random port specified by Capybara. To fix this, in yourenv.rb
file, add this line:Now when you visit a page of your app...
...the url will specify the subdomain as well as the port that the app is running on.
FYI: This worked for me using Capybara 2.0.2.
这家伙在这里有正确的答案:
http://zurb.com/forrst/posts/Testing_Subdomains_in_Capybara-g4M
你想做的事
This guy has the right answer here:
http://zurb.com/forrst/posts/Testing_Subdomains_in_Capybara-g4M
You want to do
自:
capybara-webkit (1.3.0)
as of:
capybara-webkit (1.3.0)
这与您的情况不完全相同,但这可能会对某些人有所帮助:
对于我当前的项目,我使用 pow 与许多子域。测试套件还必须在不同的端口上运行。
解决方案取决于您运行的水豚版本。
对于当前的最新版本,我将其放入 custom_env.rb 中:
使用 capybara 1.1.2,我必须进行上述更改,但
server_host
变为app_host
并修改 lib/capybara /server.rb 在 gem 中,如下所示:This is not exactly the same situation as you but this might help some people:
For my current project, I'm using pow with many subdomains. The test suite also has to run on a different port.
The solution depends on which version of capybara you're running.
For the current latest release I put this in custom_env.rb:
With capybara 1.1.2, I had had to make the above change but
server_host
becomesapp_host
AND modify lib/capybara/server.rb in the gem like this: