Ruby on rails Rspec测试轨道重定向中的test.host

Ruby on rails Rspec测试轨道重定向中的test.host,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,以下是Rails 3.1.12应用程序中我的test.rb环境文件中的一行: config.action_mailer.default_url_options = config.action_controller.default_url_options = { :host => "127.0.0.1", :port => 3000 } 下面是我做的测试: subject { get :success } subject.should redirect_to(:home) 这会产生

以下是Rails 3.1.12应用程序中我的
test.rb
环境文件中的一行:

config.action_mailer.default_url_options = config.action_controller.default_url_options = { :host => "127.0.0.1", :port => 3000 }
下面是我做的测试:

subject { get :success }
subject.should redirect_to(:home)
这会产生一个错误:

Failure/Error: subject.should redirect_to(:home)
       Expected response to be a redirect to <http://127.0.0.1:3000/> but was a redirect to <http://test.host/>

要使用Capybara设置应用程序主机和服务器端口,请在
spec/spec\u helper.rb
文件中添加以下行

Capybara.configure do |config|
  config.app_host   = 'http://127.0.0.1'
  config.server_port = 3000
end
--编辑#1

关于测试域的详细概述,请访问

这对我来说很有用:

# spec/controllers/whatever_controller_spec.rb
before :each do
  @request.host = '127.0.0.1:3000'
end
基于提供的链接@dan reedy。必须在config/environments、spec/spec\u helper.rb和这里配置完全相同的参数,这是非常烦人的。。。所有这些都以略微不同的方式(使用“http://”或不使用,端口号或端口分别指定)。即使是
Capybara.configure
语法在版本之间似乎也无法保持一致


但是试一下,看看这是否能解决问题。

这里有一个解决方法,似乎已经为我解决了这个问题:

在spec/rails\u helper.rb中添加以下内容

module ActionDispatch
  class TestRequest
    # Override host, by default it is test.host
    def host
      'localhost:3000'
    end
  end
end

这似乎对我不起作用。我已经用spec helper内容更新了这个问题。在添加水豚配置选项时,是否仍然收到相同的错误消息?是。同样的错误。为了解决这个问题,我现在使用了
home\u path
。但这并没有检查域,这是一个长期的问题。我仍然在Rails 5.2和rspec Rails 3.9上遇到这个问题。荒唐的现在使用
\u path
而不是
\u url
来绕过它。综合回答:
module ActionDispatch
  class TestRequest
    # Override host, by default it is test.host
    def host
      'localhost:3000'
    end
  end
end