具有https约束的功能测试路由

时间:2011-09-06 18:16:35

标签: testing ssl ruby-on-rails-3.1

如果我将SSL所需的路由包装在这样的块中:

scope :protocol => "https://", :constraints => { :protocol => 
"https://" } do 
  resources :users, :only => [:new, :create, :edit, :update] 
end 

然后我所有参考这些路线的功能测试现在都是 吐出路由错误。像这样:

put :update, :id => 123 
ActionController::RoutingError: No route matches 
{:id=>"123", :controller=>"users", :action=>"update"} 

FWIW:

rake routes | grep update 
user PUT    /users/:id(.:format) 
{:protocol=>"http://", :action=>"update", :controller=>"users"} 

那么,我应该在功能测试中指定协议 现在?

1 个答案:

答案 0 :(得分:0)

您可以在测试设置中启用HTTPS:

setup do
  @request.env['HTTPS'] = 'on'
end

或者您可以更改您的routes.rb以在测试环境中使用http:

scope :constraints => { :protocol => Rails.env.test? "http" : "https" } do 
  resources :users, :only => [:new, :create, :edit, :update] 
end