我看到我可以用这样的rspec测试路径:
get("/").should route_to("welcome#index")
但是我有基于主机名或部分主机名的约束以及几个主机名之间的重定向。测试时如何指定主机名?
如何使用正确的配置运行测试?我尝试打印root_url,我得到了:
缺少主机链接!请提供:host参数,设置 default_url_options [:host],或者set:only_path为true
答案 0 :(得分:53)
每当我运行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'
实际上给了我一个提示,即设计是抛出错误的那个。
原来我没有设置
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
在config/environments/test.rb
中(仅限于development.rb)
添加配置选项清除了我的错误。 我怀疑你正在使用其他需要相同选项集的宝石。
答案 1 :(得分:19)
以上都不适合我。将以下内容添加到我的spec_helper.rb(在我的案例spec / support / mailer.rb中,包含在spec_helper.rb中)修复了错误:
Rails.application.routes.default_url_options[:host] = 'test.host'
答案 2 :(得分:8)
在我的情况下,我必须添加
config.action_mailer.default_url_options = { :host => 'localhost:5000' }
跟随
config/environments/test.rb
因为我使用FactoryGirl生成用户而没有从用户那里跳过email_confirmation。