cam*_*son 10 ruby rspec rspec-rails ruby-on-rails-3
我在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, params
ActionController::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 '
你如何使用通配符(事件)处理路由?
cam*_*son 14
(回答我自己的问题)
结果证明这是一个"菜鸟"的错误.
路径的通配符(事件)部分仍然需要参数.我没有传递'event'参数,因此路线不完整.
以下代码有效:
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"
end
end
/*event(.:format) 意味着它期待一个参数.
对自己和他人的特别说明:
如果您在Rails中遇到路由错误,请验证您是否传递了所有参数.
| 归档时间: |
|
| 查看次数: |
18442 次 |
| 最近记录: |