Add MatchesController and specs for it

This commit is contained in:
Thor77 2018-11-21 18:57:16 +01:00
parent 9003f9332d
commit 6c24180715
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,14 @@
# frozen_string_literal: true
class MatchesController < ApplicationController
# GET /matches/1
def show
render json: Match.find(params[:id]), include: ['scores.score', 'scores.team'], status: status
end
private
def match_params
ActiveModelSerializers::Deserialization.jsonapi_parse(params, only: [:state])
end
end

View File

@ -2,4 +2,6 @@
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'users'
resources :matches, only: %i[show]
end

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe MatchesController, type: :controller do
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end
let(:valid_session) { {} }
before do
@match = create(:match)
@match.scores = create_pair(:score)
end
describe 'GET #show' do
it 'should return success' do
get :show, params: { id: @match.to_param }
expect(response).to be_successful
expect(response.content_type).to eq('application/json')
end
it 'should return the correct state' do
get :show, params: { id: @match.to_param }
body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body))
expect(body[:state]).to be(@match.state)
expect(body[:score_ids]).to eq(@match.scores.map { |score| score.id.to_s })
end
end
end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe MatchesController, type: :routing do
describe 'routing' do
it 'routes to #show' do
expect(get: '/matches/1').to route_to('matches#show', id: '1')
end
end
end