Pablo Fernandez
Pablo Fernandez

Reputation: 287430

rspec route testing and hosts

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

Upvotes: 20

Views: 11977

Answers (3)

jgrevich
jgrevich

Reputation: 301

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'

Upvotes: 20

Kryptman
Kryptman

Reputation: 1016

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.

Upvotes: 8

Rystraum
Rystraum

Reputation: 2025

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.

Upvotes: 51

Related Questions