rspec 路由测试和主机 [英] rspec route testing and hosts

查看:37
本文介绍了rspec 路由测试和主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

如何以正确的配置运行测试?我尝试打印 root_url 并得到:

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

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

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to 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'

实际上给了我一个提示,即设计是抛出错误的那个.

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

原来我还没有设置

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

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

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

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

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

这篇关于rspec 路由测试和主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆