在我的应用程序中运行良好的路由在 rspec 测试中使用“无路由匹配”进行任何 get/put 调用都会失败。我在这里做错了什么?
这是一个简单的例子,来自contracts_controller_spec.rb:
it 'should redirect to edit on show' do
get :show
response.should be_success
response.should render_template(:edit)
end
以上失败并出现以下情况:
ContractsController api calls should redirect to edit on show
Failure/Error: get :show
ActionController::RoutingError:
No route matches {:controller=>"contracts", :action=>"show"}
show
contract_controller.rb中的方法:
def show
Rails.logger.debug("getting contract info....")
get_contract_info
Rails.logger.debug("...got contract info.")
render :action => :edit
end
routes.rb 内容:
resource :contract, :only=>[:show, :edit, :update], :protocol =>ROUTES_PROTOCOL do
member do
get :print
end
end
rake routes
输出:
print_contract GET /contract/print(.:format) contracts#print {:protocol=>"http"}
edit_contract GET /contract/edit(.:format) contracts#edit {:protocol=>"http"}
contract GET /contract(.:format) contracts#show {:protocol=>"http"}
PUT /contract(.:format) contracts#update {:protocol=>"http"}
- 使用 rspec-rails 2.14.0
- 这个应用程序最近从 Rails 2.3 升级到 3.2,否则会成功
- 请注意非标准的显示/编辑路线:不需要 id,并且强制使用 id 仍然会导致
No route matches {:controller=>"contracts", :id=>"1", :action=>"show"}