2017-07-27 36 views
-1

所有!正確地在rspec中提出要求

我想知道如何從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 

回答

0

由於Rails 5你需要提供params選項和id必須user PARAMS之外:

describe 'PUT /users/:id' do 
    context '# check success update' do 

    before do 
     put 'update', params: { id: 3, user: { email: '[email protected]'} } 
    end 

    it 'returns status code 204' do 
     expect(response).to have_http_status(204) 
    end 
    end 
end 
+0

伊戈爾,我怎麼能自己找到它,我的意思是在文檔中。我試過api.rubydoc,rails指南,並且在文檔中找不到任何有關這方面的信息。魔術文檔;)。請分享您的經驗:尋找文檔並解決類似問題的'rails方式'是什麼? – fernal9301

+0

@ fernal9301:「用戶散列之外的id」只是常識。這就是軌道的工作原理。你看到路由中的'PUT/users /:id(。:format)'?這告訴你rails需要'params [:id]',而不是'params [:user] [:id]'。可能沒有單個文檔頁面或指南。你需要結構化的知識。獲得一本好的滑軌書。 –

+0

@ fernal9301:和「你現在必須通過'params'鍵傳遞params」 - 這是在當前版本的rails指南中:) http://guides.rubyonrails.org/testing.html#functional-tests-for-您的控制器 –