Url 助手(例如 root_url)在应用程序 Controller 与 rspec 示例中返回不同的主机名。
我已经成功地在我的 rails 应用程序中为 url 助手设置了域,如下所示:
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 选项不需要是动态的。
最佳答案
在功能规范中,主机!已被弃用。将这些添加到您的 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
关于ruby-on-rails - rails : Wrong hostname for url helpers in rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10521201/