【问题标题】:test.host in Rspec testing Rails redirectRspec 中的 test.host 测试 Rails 重定向
【发布时间】:2013-03-14 16:27:32
【问题描述】:

这是我在 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/>

我做错了什么?或者,测试主机在哪里配置?

这里是 spec_helper.rb 文件供完整参考。

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end
Capybara.configure do |config|
  config.app_host    = 'http://127.0.0.1'
  config.server_port = 3000
end

【问题讨论】:

  • 我仍然在 Rails 5.2 和 rspec-rails 3.9 上遇到这个问题。荒谬的。现在使用_path 而不是_url 来解决它。

标签: ruby-on-rails rspec capybara


【解决方案1】:

要使用 Capybara 设置应用程序主机和服务器端口,请将以下行添加到您的 spec/spec_helper.rb 文件中

Capybara.configure do |config|
  config.app_host   = 'http://127.0.0.1'
  config.server_port = 3000
end

-- 编辑#1

http://blog.joncairns.com/2012/12/testing-domains-with-rails-and-test-unit/ 上提供了有关测试域的精彩概述

【讨论】:

  • 这似乎对我不起作用。我已使用规范帮助程序内容更新了问题。
  • 添加 Capybara 配置选项时是否仍然收到相同的错误消息?
  • 是的。同样的错误。为了解决这个问题,我现在使用home_path。但这不会检查域,从长远来看这是一个问题。
【解决方案2】:

这是似乎为我解决了问题的解决方法:

在 spec/rails_helper.rb 中添加这个

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

【讨论】:

    【解决方案3】:

    这对我有用:

    # spec/controllers/whatever_controller_spec.rb
    before :each do
      @request.host = '127.0.0.1:3000'
    end
    

    基于@dan-reedy 提供的链接。必须在 config/environments、spec/spec_helper.rb 和这里再次配置完全相同的参数是非常烦人的……所有这些都以略微不同的方式(使用“http://”或不使用,指定端口号或端口)分别地)。即使Capybara.configure 语法似乎也无法在版本之间保持一致...

    但是试一试,看看能不能解决问题。

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多