Rails 路径助手没有为请求选择应用程序主机

Rails Path Helpers not selecting application host for requests

我的规格不合格,当我去检查它们时,所有请求都已发送给 http://www.example.com。因此,对我来说很明显,某些东西正在覆盖默认主机,但是当我为 example.com 的实例 grep 应用程序时,我找不到任何导致它将请求定向到那里的东西,相反,我有两个地方明确地将请求定向到本地主机

spec/rails_helper.rb

  config.before(:each) do
    host! "https://localhost:3000"
  end

config/environments/test.rb

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

关于什么可以覆盖它以及我如何更积极地强制路由助手使用 localhost 作为主机有什么想法吗?

正如@rmlockerd 上面提到的,这似乎回到了一个关于动作调度的老问题,这个问题可能以某种方式重新浮出水面 Rails 6

这是由 ActionDispatch::Integration::Session 中的这段代码造成的,它将 example.com 硬编码为 DEFAULT_HOST

https://github.com/rails/rails/blob/d45baa34445379319f5c46f6d13f52e6dd79bc9a/actionpack/lib/action_dispatch/testing/integration.rb#L84

在这个 rails 问题 https://github.com/rails/rails/issues/546

中有一些进一步的信息

鉴于此,您可以猴子修补 class 以将您的默认主机和协议设置为任何您想要的。可能很糟糕,我相信有更好的方法,但我不知道它是什么。

所以在我的 rails_helper.rb 的主要范围内,我添加了

module ActionDispatch
  module Integration #:nodoc:
    class Session
      DEFAULT_HOST = "localhost"

      def https?
        true
      end
    end
  end
end