gpt4 book ai didi

ruby-on-rails - Rails 3.1 在测试环境中设置主机

转载 作者:行者123 更新时间:2023-12-05 01:18:06 26 4
gpt4 key购买 nike

我收到 http://www.example.com每当我使用 root_url在我的测试中。

它在开发中运行良好,我在 config/environments/development.rb 中有这个:

Rails.application.routes.default_url_options[:host]= 'localhost:3000'

但是,在 config/environments/test.rb 中添加它不起作用。我应该添加什么来使用 localhost:3000作为测试环境中的主机?

最佳答案

测试依赖于 default_url_options 的代码导致各种问题,见this thread而这个 issue举些例子。

我已经通过修补 ActionDispatch::Routing::RouteSet 解决了这个问题在测试中强制 rails 包含我想要的任何选项的默认值(在我的情况下 locale )。查看我在 github issue 中的回答链接到上面的详细信息。

覆盖 host使用相同方法的选项:

class ActionDispatch::Routing::RouteSet
def url_for_with_host_fix(options)
url_for_without_host_fix(options.merge(:host => 'localhost:3000'))
end

alias_method_chain :url_for, :host_fix
end

将其放入 support 中的文件中,应该可以解决问题。

关于ruby-on-rails - Rails 3.1 在测试环境中设置主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com