作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 Rails 中有这条(公认的丑陋的)路线:
scope '/software' do post '/:software_id/:attachment_id/event/*event' => 'software#post_event', as: 'post_event'end
(如果不是遗留 API,我会更改它)
我正在为它编写一个 RSpec 测试。
rake routes
给我:
post_event POST /software/:software_id/:attachment_id/event/*event(.:format) api/version1301/software#post_event
我的测试是这样的:
describe "post_event" do it "should respond with 204" do params = { attachment_id: @attachment.uid, software_id: @license.id } post :post_event, params response.code.should eq "204" end end
但我收到以下路由错误:
Failure/Error: post :post_event, paramsActionController::RoutingError:No route matches {:format=>"json", :name_path=>:api, :attachment=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :software_id=>"0000001", :attachment_id=>"7b40ab6a-d522-4a86-b0de-dfb8081e4465", :controller=>"api/version1301/software", :action=>"post_event"} # ./spec/controllers/api/version1301/software_controller_spec.rb:62:in `block (4 levels) in '
如何处理带有通配符(事件)的路由?
最佳答案
(回答我自己的问题)
事实证明这是一个有点“新手”的错误。
路由的通配符(事件)部分仍然需要一个参数。我没有传递“事件”参数,因此路线不完整。
以下代码有效:
describe "post_event" do it "should respond with 204" do params = { attachment_id: @attachment.uid, software_id: @license.id, event: 'some/event' } post :post_event, params response.code.should eq "204" endend
/*event(.:format)
表示它需要一个参数。
对自己和他人的特别说明:
如果您在 Rails 中遇到路由错误,请验证您是否传递了所有参数。
关于ruby - 如何在具有通配符的 RSpec 中请求(GET/POST)路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072284/
我是一名优秀的程序员,十分优秀!