rspec路由测试和主机

我看到我可以像这样用rspec测试路由:

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

但是我有基于主机名或主机名部分的约束以及几个主机名之间的重定向。 如何在测试时指定主机名?

如何使用正确的配置运行测试? 我试过打印root_url,我得到了:

缺少主机链接! 请提供:host参数,设置default_url_options [:host],或将:only_path设置为true

每当我运行rspec spec/时,我的同样的错误就会发生

整个错误实际上是:

 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 

以下行:

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

实际上给了我一个暗示,设计是抛出错误的那个。

事实certificate我没有设定

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

config/environments/test.rb (仅在development.rb中)

添加配置选项清除了我的错误。 我怀疑你正在使用其他需要相同选项集的gem。

以上都不适合我。 将以下内容添加到我的spec_helper.rb(在我的案例spec / support / mailer.rb中,包含在spec_helper.rb中)修复了错误:

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

在我的情况下,我不得不添加

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

跟随

 config/environments/test.rb 

因为我使用FactoryGirl生成用户而没有从用户那里跳过email_confirmation。