From 223a6e7a85b8a42492f97a990335c9a01e10c645 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Mon, 4 May 2015 02:14:37 +0000 Subject: [PATCH] Handle save error in API playlists/update --- app/controllers/api/playlists_controller.rb | 7 +++- spec/integration/api/playlists_spec.rb | 43 ++++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/playlists_controller.rb b/app/controllers/api/playlists_controller.rb index 5bb721e..90300c5 100644 --- a/app/controllers/api/playlists_controller.rb +++ b/app/controllers/api/playlists_controller.rb @@ -19,8 +19,11 @@ module API end def update - @playlist.update playlist_params - head :no_content + if @playlist.update playlist_params + head :no_content + else + render json: @playlist.errors, status: :unprocessable_entity + end end def destroy diff --git a/spec/integration/api/playlists_spec.rb b/spec/integration/api/playlists_spec.rb index 992b09d..3ad78ac 100644 --- a/spec/integration/api/playlists_spec.rb +++ b/spec/integration/api/playlists_spec.rb @@ -56,15 +56,40 @@ describe 'API playlists' do end end - it 'updates a playlist' do - playlist = create :playlist - put api_playlist_path(playlist), format: :json, playlist: { - name: 'new name' - } - get api_playlist_path playlist, format: :json - expect(json[:playlist]).to include( - name: 'new name' - ) + describe 'playlist update' do + let(:name) { 'new name' } + let(:playlist) { create :playlist } + + before do + put api_playlist_path(playlist), format: :json, playlist: { + name: name + } + end + + it 'responds with no content status' do + expect(response).to have_http_status 204 + end + + it 'updates the playlist' do + get api_playlist_path playlist, format: :json + expect(json[:playlist]).to include( + name: 'new name' + ) + end + + context 'when playlist is invalid' do + let(:name) { '' } + + it 'responds with unprocessable entity status' do + expect(response).to have_http_status 422 + end + + it 'returns errors' do + expect(json 422).to match( + name: [an_instance_of(String)] + ) + end + end end it 'destroys a playlist' do