rspec route testing and hosts rspec route testing and hosts ruby ruby

rspec route testing and hosts


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.


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'


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.