From dcb8e6b2166b0052ccdb7f53ff043699bb08f636 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Tue, 11 Dec 2018 16:34:39 +0100 Subject: [PATCH] Adapt controller specs to new api schema --- .../match_scores_controller_spec.rb | 21 ++++------ spec/controllers/matches_controller_spec.rb | 4 +- spec/controllers/teams_controller_spec.rb | 18 +++----- .../tournaments_controller_spec.rb | 41 +++++++------------ 4 files changed, 29 insertions(+), 55 deletions(-) diff --git a/spec/controllers/match_scores_controller_spec.rb b/spec/controllers/match_scores_controller_spec.rb index d76263b..f2429ce 100644 --- a/spec/controllers/match_scores_controller_spec.rb +++ b/spec/controllers/match_scores_controller_spec.rb @@ -18,21 +18,14 @@ RSpec.describe MatchScoresController, type: :controller do get :show, params: { id: @match_score.to_param } body = deserialize_response response expect(body[:points]).to eq(@match_score.points) - expect(body[:team_id]).to eq(@match_score.team.id.to_s) - expect(body[:match_id]).to eq(@match_score.match.id.to_s) + expect(body[:team][:id]).to eq(@match_score.team.id) end end describe 'PUT #update' do let(:valid_update) do { - data: { - id: @match_score.id, - type: 'match_scores', - attributes: { - points: 42 - } - } + points: 42 } end @@ -43,16 +36,16 @@ RSpec.describe MatchScoresController, type: :controller do end it 'updates the requested score' do - put :update, params: { id: @match_score.to_param }.merge(valid_update) + put :update, params: { id: @match_score.to_param, match_score: valid_update } @match_score.reload - expect(@match_score.points).to eq(valid_update[:data][:attributes][:points]) + expect(@match_score.points).to eq(valid_update[:points]) end it 'renders a response with the updated team' do - put :update, params: { id: @match_score.to_param }.merge(valid_update) + put :update, params: { id: @match_score.to_param, match_score: valid_update } expect(response).to be_successful body = deserialize_response response - expect(body[:points]).to eq(valid_update[:data][:attributes][:points]) + expect(body[:points]).to eq(valid_update[:points]) end end @@ -62,7 +55,7 @@ RSpec.describe MatchScoresController, type: :controller do end it 'renders a forbidden error response' do - put :update, params: { id: @match_score.to_param }.merge(valid_update) + put :update, params: { id: @match_score.to_param, match_score: valid_update } expect(response).to have_http_status(:forbidden) end end diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index d05e1b9..4426afa 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -17,9 +17,9 @@ RSpec.describe MatchesController, type: :controller do it 'should return the correct state' do get :show, params: { id: @match.to_param } - body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body)) + body = deserialize_response response expect(body[:state]).to eq(@match.state) - expect(body[:match_score_ids]).to eq(@match.match_scores.map { |match_score| match_score.id.to_s }) + expect(body[:match_scores].map { |ms| ms[:id] }).to eq(@match.match_scores.map(&:id)) end end end diff --git a/spec/controllers/teams_controller_spec.rb b/spec/controllers/teams_controller_spec.rb index 2537aa7..db124d2 100644 --- a/spec/controllers/teams_controller_spec.rb +++ b/spec/controllers/teams_controller_spec.rb @@ -24,13 +24,7 @@ RSpec.describe TeamsController, type: :controller do describe 'PUT #update' do let(:valid_update) do { - data: { - id: @team.id, - type: 'teams', - attributes: { - name: Faker::Dog.name - } - } + name: Faker::Dog.name } end @@ -40,17 +34,17 @@ RSpec.describe TeamsController, type: :controller do end it 'updates the requested team' do - put :update, params: { id: @team.to_param }.merge(valid_update) + put :update, params: { id: @team.to_param, team: valid_update } @team.reload expect(response).to be_successful - expect(@team.name).to eq(valid_update[:data][:attributes][:name]) + expect(@team.name).to eq(valid_update[:name]) end it 'renders a response with the updated team' do - put :update, params: { id: @team.to_param }.merge(valid_update) + put :update, params: { id: @team.to_param, team: valid_update } expect(response).to be_successful body = deserialize_response response - expect(body[:name]).to eq(valid_update[:data][:attributes][:name]) + expect(body[:name]).to eq(valid_update[:name]) end end @@ -60,7 +54,7 @@ RSpec.describe TeamsController, type: :controller do end it 'renders a forbidden error response' do - put :update, params: { id: @team.to_param }.merge(valid_update) + put :update, params: { id: @team.to_param, team: valid_update } expect(response).to have_http_status(:forbidden) end end diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index d6bec62..8c4c69f 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -4,7 +4,7 @@ require 'rails_helper' RSpec.describe TournamentsController, type: :controller do def tournament_ids(response) - deserialize_list(response).map { |t| t[:id].to_i } + deserialize_response(response).map { |t| t[:id].to_i } end before do @tournament = create(:tournament) @@ -22,14 +22,14 @@ RSpec.describe TournamentsController, type: :controller do it 'returns all public tournaments' do get :index - tournaments = deserialize_list response + tournaments = deserialize_response response public_tournaments = tournaments.select { |t| t[:public] } expect(public_tournaments.size).to eq((Tournament.where public: true).size) end it 'returns no private tournaments for unauthenticated users' do get :index - tournaments = deserialize_list response + tournaments = deserialize_response response private_tournaments = tournaments.reject { |t| t[:public] } expect(private_tournaments.size).to eq(0) end @@ -62,18 +62,11 @@ RSpec.describe TournamentsController, type: :controller do describe 'POST #create' do let(:create_data) do { - data: { - type: 'tournaments', - attributes: { - name: Faker::Dog.name, - description: Faker::Lorem.sentence, - public: false - }, - relationships: { - teams: { - data: @teams.map { |team| { type: 'teams', id: team.id } } - } - } + name: Faker::Dog.name, + description: Faker::Lorem.sentence, + public: false, + teams: { + data: @teams.map { |team| { type: 'teams', id: team.id } } } } end @@ -116,20 +109,14 @@ RSpec.describe TournamentsController, type: :controller do describe 'PUT #update' do let(:valid_update) do { - data: { - type: 'tournaments', - id: @tournament.id, - attributes: { - name: Faker::Dog.name - } - } + name: Faker::Dog.name } end context 'with valid params' do context 'without authentication headers' do it 'renders a unauthorized error response' do - put :update, params: { id: @tournament.to_param }.merge(valid_update) + put :update, params: { id: @tournament.to_param, tournament: valid_update } expect(response).to have_http_status(:unauthorized) end end @@ -140,13 +127,13 @@ RSpec.describe TournamentsController, type: :controller do end it 'updates the requested tournament' do - put :update, params: { id: @tournament.to_param }.merge(valid_update) + put :update, params: { id: @tournament.to_param, tournament: valid_update } @tournament.reload - expect(@tournament.name).to eq(valid_update[:data][:attributes][:name]) + expect(@tournament.name).to eq(valid_update[:name]) end it 'renders a JSON response with the tournament' do - put :update, params: { id: @tournament.to_param }.merge(valid_update) + put :update, params: { id: @tournament.to_param, tournament: valid_update } expect(response).to have_http_status(:ok) expect(response.content_type).to eq('application/json') end @@ -158,7 +145,7 @@ RSpec.describe TournamentsController, type: :controller do end it 'renders a forbidden error response' do - put :update, params: { id: @tournament.to_param }.merge(valid_update) + put :update, params: { id: @tournament.to_param, tournament: valid_update } expect(response).to have_http_status(:forbidden) end end