-1
我想知道如何从RSpec的使PUT/PATCH请求以测试的Rails 5.1.2 API,这样的:
describe 'PUT /users/:id' do
context '# check success update' do
it 'returns status code 204' do
put 'update', params: { user: { id: 3, email: '[email protected]'} }
expect(response).to have_http_status(204)
end
end
end
得到了以下错误:
的ActionController :: UrlGenerationError:
No route matches {:action=>"update", :controller=>"users", :user=>{:id=>3, :email=>"[email protected]"}}
路线都ok:
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
伊戈尔,我怎么能自己找到它,我的意思是在文档中。我试过api.rubydoc,rails指南,并且在文档中找不到任何有关这方面的信息。魔术文档;)。请分享您的经验:寻找文档并解决类似问题的'rails方式'是什么? – fernal9301
@ fernal9301:“用户散列之外的id”只是常识。这就是轨道的工作原理。你看到路由中的'PUT/users /:id(。:format)'?这告诉你rails需要'params [:id]',而不是'params [:user] [:id]'。可能没有单个文档页面或指南。你需要结构化的知识。获得一本好的滑轨书。 –
@ fernal9301:和“你现在必须通过'params'键传递params” - 这是在当前版本的rails指南中:) http://guides.rubyonrails.org/testing.html#functional-tests-for-您的控制器 –