0
votes

I have built a module in Solidus and ruby on rails and now I try to create tests.

I am trying to create some integration tests and I have this error:

E Error: PrescriptionPhotosTest#test_/prescription_photos_post_endpoint_throws_an_error_when_wrong_params: ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true test/integration/prescription_photos_test.rb:6:in `block in '

I read this this and this, but it doesn't make any difference.

My code is returning this error on this line:

@user = FactoryGirl.create(:user)

Here is the code from my files.

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

After running rake test:integration I got the error mentioned above.

I did a lot of googleing and I tried to fix this problem in the past 2 days, but I had no luck.

1
So I'm assuming you tried with using a default_url_options in the test.rb environment config and that it didn't solve it. Have you tried with config.action_mailer.delivery_method = :testm3characters
It is already there. I updated the question and I added also the code from test.rb @MicaelNussbaumerThe user without number
You shouldn't even need .default_url_options I think. But you have it set in two different places, can you remove the second one where you're not specifying the :port?m3characters
@MicaelNussbaumer, I did and I have exactly the same problem.The user without number
I would say to remove the host configuration in test.rb, since it's not the cause. I have never used Spree so not sure if it's that that triggers the email sending on user creation? Perhaps you should look into that?m3characters

1 Answers

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

This line of code from prescription_photos_rest.rb was overwritten buy a env value.