我看到我可以像这样用 rspec 测试路由:
get("/").should route_to("welcome#index")
但我有基于主机名或部分主机名的约束,并在多个主机名之间重定向。测试时如何指定主机名?
如何使用正确的配置运行测试?我尝试打印 root_url 并得到:
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'
实际上给了我设计是抛出错误的暗示。
原来我没有设置
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
在 config/environments/test.rb
中(仅在 development.rb 中)
添加配置选项清除了我的错误。 我怀疑您正在使用其他需要相同选项集的 gem。
关于ruby-on-rails - rspec 路由测试和主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7182600/