20

I see I can test routes with rspec like this:

get("/").should route_to("welcome#index")

but I have constraints based on the hostname or parts of hostnames and redirects between several ones. How do I specify a hostname when testing?

How do I run the tests with proper configuration? I tried printing root_url and I got:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

3 Answers3

52

The same error happens on mine whenever I run rspec spec/

The entire error is actually:

Failure/Error: @user = Factory(:user)
     ActionView::Template::Error:
       Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
     # ./app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb__980323237__638702928'
     # ./spec/models/campaign_spec.rb:21

The following line:

# ./app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb__980323237__638702928'

actually gave me the hint that devise is the one throwing out the error.

Turns out I haven't set

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

in config/environments/test.rb (only in development.rb)

Adding the config option cleared out the errors on mine. I'm suspecting you're using other gems that requires the same option set.

Rystraum
  • 1,985
  • 1
  • 20
  • 32
  • I'm not using devise, and I got the same error when I was trying to use something_url instead of something_path in one of my mailer views - because actionmailer doesn't have any idea what sort of environment its running in. Your solution works, but I just wanted to clarify that it is not caused by devise. – Oranges13 Mar 07 '12 at 20:43
  • 2
    @Oranges13: 6 months after and I can clearly see that it's not devise causing the error, it's just that it passed through devise' views. I'm kind of happy I documented my whole debugging process, makes me realize how much Rails I've learned. – Rystraum Mar 27 '12 at 05:18
  • 1
    @Rystraum I could resolve the issue with your solution. However, assuming that your User model includes Devise's `:confirmable` module then in your factory definition for `:user` setting confirmed_at attribute in following manner `confirmed_at { Time.now }` prevents the *Missing host...* error. Nevertheless setting default host is required when our code involves generating absolute urls for the links. Thanks. – Jignesh Gohel Mar 23 '15 at 08:23
  • @JiggneshhGohel Thank you for adding that comment, you have saved me so much time and stress. This solved it for me when nothing else would. – rmcsharry Apr 13 '16 at 09:26
20

None of the above worked for me. Adding the following to my spec_helper.rb (in my case spec/support/mailer.rb which is included in spec_helper.rb) fixed the error:

Rails.application.routes.default_url_options[:host] = 'test.host'

jgrevich
  • 301
  • 2
  • 5
8

In my case I had to add

 config.action_mailer.default_url_options = { :host => 'localhost:5000' }

to following to

config/environments/test.rb

because I was using FactoryGirl to generate a user without skipping the email_confirmation from user.

Kryptman
  • 996
  • 9
  • 14