Rails:rspec中URL助手的主机名错误 [英] Rails: Wrong hostname for url helpers in rspec

查看:50
本文介绍了Rails:rspec中URL助手的主机名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

URL助手(例如root_url)在app控制器与rspec示例中返回不同的主机名。
我已经成功地能够在我的rails应用程序中为URL助手设置域,如下所示:

Url helpers (e.g root_url) returns a different hostname in the app controllers vs rspec examples. I have successfully been able to set the domain for url helpers in my rails app like this:

class ApplicationController < ActionController::Base
  def default_url_options
    {host: "foo.com", only_path: false}
  end
end

例如,root_url在应用程序中输出 http://foo.com/,但在我的请求规范中输出 http://example.com。建议在Rails 3.2中全局应用这些url选项以使其影响我的规格的推荐方法是什么?
url选项不必是动态的。

For example, root_url outputs "http://foo.com/" within the application but "http://example.com" in my request specs. What is the recommended way to apply those url options globally in rails 3.2 so that they affect my specs? The url options do not need to be dynamic.

推荐答案

在功能规格中,主机!已不推荐使用。将这些添加到您的rspec_helper.rb中:

In Feature specs, host! has been deprecated. Add these to your rspec_helper.rb:

# Configure Capybara expected host
Capybara.app_host = 'http://lvh.me:3000'

# Configure actual routes host during test
before(:each) do
  if respond_to?(:default_url_options)
    default_url_options[:host] = 'http://lvh.me:3000'
  end
end

这篇关于Rails:rspec中URL助手的主机名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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