2017-07-25 25 views
0

我已經在Solidus和ruby on rails中構建了一個模塊,現在我嘗試創建測試。ArgumentError:缺少主機鏈接!請提供:host參數,設置default_url_options [:host],或者設置:only_path爲true

我試圖創造一些集成測試,我有這樣的錯誤:

è 錯誤: PrescriptionPhotosTest#TEST_/prescription_photos_post_endpoint_throws_an_error_when_wrong_params: 引發ArgumentError:缺少主機鏈接到!請提供:主機參數,設置default_url_options [:主機],或設置:only_path爲true 測試/集成/ prescription_photos_test.rb:6:'塊'

我讀到這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 '[email protected]' 
    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 

運行耙測試後:集成我上面提到的錯誤。

我做了很多googleing,我在過去的2天裏試圖解決這個問題,但是我沒有運氣。

+0

所以我假設你在test.rb環境配置中使用了default_url_options,並且它沒有解決它。你有沒有嘗試'config.action_mailer.delivery_method =:test' –

+0

它已經在那裏。我更新了問題,並添加了test.rb的代碼@MicaelNussbaumer –

+0

我認爲你甚至不需要'.default_url_options'。但是你把它設置在兩個不同的地方,你可以刪除第二個你沒有指定':port'的地方嗎? –

回答

0
Spree::Auth::Config[:confirmable] = false 

這行代碼從prescription_photos_rest.rb被覆蓋購買env值。

相關問題