How can I specify https protocol in routing spec with rspec?

岁酱吖の 提交于 2019-12-07 03:04:07

问题


In my routes file I have:

resources :subscription, :only => [:show], :constraints => {:protocol => "https"}

I'm trying to add a spec for this route like this:

it "recognizes and generates #show" do
  { :get => "/subscription", :protocol => 'https' }.should route_to(:controller => "subscriptions", :action => "show")
end

However, the spec still fails. If I remove the :protocol => 'https', the spec also fails:

ActionController::RoutingError:    
  No route matches "/subscription" 

回答1:


The (undocumented?) solution is to simply include an entire dummy url, like so:

it "recognizes and generates #show" do
  { :get => "https://test.host/subscription" }.should route_to(:controller => "subscriptions", :action => "show")
end

I figured it out from this ticket and this changeset.




回答2:


I'm not sure, but I think routes should be declared as plural — see Rails Routing from the Outside In. So it would be

resources :subscriptions

and in the spec

{:get => '/subscriptions', :protocol => 'https'}

Try if that passes without :protocol. If it does, skip the spec with HTTPS. That should not be tested on a unit-test level, but in an integration test.




回答3:


Does it pass if you change to spec to this?:

{:get => '/subscription'}.should_not route_to(:controller => …)

That would at least give you the confidence that HTTP is not routed.



来源:https://stackoverflow.com/questions/7317761/how-can-i-specify-https-protocol-in-routing-spec-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!