【问题标题】:ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to trueArgumentError:缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true
【发布时间】:2017-12-31 09:23:47
【问题描述】:

我已经在 Solidus 和 ruby​​ on rails 中构建了一个模块,现在我尝试创建测试。

我正在尝试创建一些集成测试,但出现此错误:

E 错误: PrescriptionPhotosTest#test_/prescription_photos_post_endpoint_throws_an_error_when_wrong_params: ArgumentError:缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true test/integration/prescription_photos_test.rb:6:in `block in '

我读过这个thisthis,但没有任何区别。

我的代码在这一行返回此错误:

@user = FactoryGirl.create(:user)

这是我文件中的代码。

prescription_photos_rest.rb

require 'test_helper'

class PrescriptionPhotosTest < ActionDispatch::IntegrationTest
  setup do
    Spree::Auth::Config[:confirmable] = false
    @user = FactoryGirl.create(:user)
  end

  def authenticated_header
    token = Knock::AuthToken.new(payload: { sub: @user.id }).token
    {
        'Authorization': "Bearer #{token}"
    }
  end

  test '/prescription_photos post endpoint throws an error when wrong params' do
     post '/api/prescription_photos', headers: authenticated_header, params: {
       something: {
           is: 'wrong'
       }
     }

    json = JSON.parse(body)

     assert_response 422
     assert json.has_key? 'exception'
     assert_equal 'param is missing or the value is empty: prescription', json['exception']
  end
end

factories.rb

FactoryGirl.define do
  factory :user, class: Spree::User do
    email 'test@test.nl'
    password 'test123'
    password_confirmation 'test123'
  end
end

test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
  # Add more helper methods to be used by all tests here...
end

test.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  config.action_controller.default_url_options = { host: 'localhost', port: 3000 }

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Do not eager load code on boot. This avoids loading your whole application
  # just for the purpose of running a single test. If you are using a tool that
  # preloads Rails for running tests, you may have to set it to true.
  config.eager_load = false

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true
  config.public_file_server.headers = {
    'Cache-Control' => 'public, max-age=3600'
  }

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false
  config.action_mailer.perform_caching = false

  # Add the host parameter
  config.action_mailer.default_url_options = { host: 'localhost' }

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test

  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

运行 rake test:integration 后出现上述错误。

我在过去 2 天内进行了很多谷歌搜索,并尝试解决此问题,但没有成功。

【问题讨论】:

  • 所以我假设您尝试在 test.rb 环境配置中使用 default_url_options 并且它没有解决它。你试过config.action_mailer.delivery_method = :test
  • 它已经在那里了。我更新了问题,还添加了来自 test.rb @MicaelNussbaumer 的代码
  • 我认为你甚至不需要.default_url_options。但是你把它设置在两个不同的地方,你能把第二个没有指定:port的地方去掉吗?
  • @MicaelNussbaumer,我确实遇到了同样的问题。
  • 我会说删除test.rb 中的主机配置,因为这不是原因。我从未使用过 Spree,所以不确定是否会触发在用户创建时发送电子邮件?也许你应该调查一下?

标签: ruby-on-rails spree solidus


【解决方案1】:
Spree::Auth::Config[:confirmable] = false

prescription_photos_rest.rb 中的这行代码被覆盖购买 env 值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2011-05-06
    • 2016-07-08
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多