在rspec中的控制器测试中使用rails“post”,并在路由上使用作用域和协议

我有一个rails项目,它正在生产中基本URL的子目录中运行,我希望它在dev中以这种方式运行,以使dev和prod尽可能接近。 我有路由文件设置如下:

Foo::Application.routes.draw do def draw_routes root :to=>'foo#home' resources :cats, :only=>[:create] do end if Rails.env.production? scope :protocol=>'http://' do draw_routes end else scope :path=>"/foo", :protocol=>'http://' do draw_routes end end end 

我的CatsController是这样的:

 class CatsController "Bob") if @cat.save() redirect_to root end end end 

我想测试我的Create Cat方法,所以我在spec / controllers / cats_controller_spec.rb中设置了一个rspec测试:

 require 'spec_helper' describe CatsController do describe "calling create cat with good data" do it "should add a cat" do expect {post(:action=>:create)}.to change(Cat, :count).by(1) end end end 

但是,当我进行测试时,我得到了

 Failure/Error: post :create ActionController::RoutingError: No route matches {:controller=>"cats", :action=>"create"} # ./spec/controllers/cats_controller_spec.rb:5:in `(root)' 

耙路线说我的路线在那里! 引擎盖下发生了什么,为什么会失败?

 Rake Routes: cats POST /foo/cats(.:format) cats#create {:protocol=>"http://"} 

问题是:protocol的价值有点不稳定。

我认为,更好的解决方法是将范围中的协议设置为http而不是http:// 。 但是,如果您确实需要使用一些时髦的协议来测试控制器,那么您应该能够:

 post(:action => :create, :protocol => 'funky') 

或者你的协议可能是什么。

对于rspec 3,以下内容适用于我。

 post :action, protocol: 'https://'