diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 9e3a26e..c034a1a 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -5,8 +5,9 @@ require 'rails_helper' RSpec.describe MatchesController, type: :controller do before do @match = create(:match, state: :not_started) - @tournament = create(:stage_tournament, stage_count: 2) - @running_playoff_match = @tournament.stages.find_by(level: 2).matches.first + @amount_of_stages = 2 + @tournament = create(:stage_tournament, stage_count: @amount_of_stages) + @running_playoff_match = @tournament.stages.find_by(level: @amount_of_stages).matches.first @match.match_scores = create_pair(:match_score) end @@ -83,11 +84,16 @@ RSpec.describe MatchesController, type: :controller do expect(@running_playoff_match.state).to eq(finished[:state]) end - it 'populates the match below' do - match_below = @tournament.stages.find { |s| s.level == 2 }.matches - .find { |m| m.position == @running_playoff_match.position / 2 }.reload - expect(@running_playoff_match.winner).to be_a(Team) - expect(match_below.teams).to include(@running_playoff_match.winner) + describe 'updates the match below' do + before do + @match_below = @tournament.stages.find_by(level: @amount_of_stages - 1).matches + .find_by(position: @running_playoff_match.position / 2).reload + end + + it 'with the right teams' do + expect(@running_playoff_match.winner).to be_a(Team) + expect(@match_below.teams).to include(@running_playoff_match.winner) + end end end end diff --git a/spec/services/playoff_stage_service_spec.rb b/spec/services/playoff_stage_service_spec.rb index 3e8812f..ccab2d3 100644 --- a/spec/services/playoff_stage_service_spec.rb +++ b/spec/services/playoff_stage_service_spec.rb @@ -100,57 +100,67 @@ RSpec.describe PlayoffStageService do end context 'match below has no match_scores' do - it 'finds the correct match and adds two new match_scores to it' do + before do @match_to_find.match_scores = [] @match_to_find.save - PlayoffStageService.populate_match_below(@match) - @match_to_find.reload + @test = PlayoffStageService.populate_match_below(@match).first + end + + it 'finds the correct match and adds two new match_scores to it' do expect(@match_to_find.teams).to match_array(@match.winner) end end context 'match below has one match_score with the winning team' do - it 'finds the correct match and adds no match_score' do + before do @match_to_find.match_scores = create_list(:match_score, 1, team: @match.winner) @match_to_find.save - PlayoffStageService.populate_match_below(@match) - @match_to_find.reload - expect(@match_to_find.teams).to match_array(@match.winner) + @test = PlayoffStageService.populate_match_below(@match).first + end + + it 'finds the correct match and adds no match_score' do + expect(@test.teams).to match_array(@match.winner) end end context 'match below has one match_score with an unknown team' do - it 'finds the correct match and replaces the match_score' do + before do @match_to_find.match_scores = create_list(:match_score, 1, team: create(:team), points: 1337) @match_to_find.save - PlayoffStageService.populate_match_below(@match) - @match_to_find.reload - expect(@match_to_find.teams).to match_array(@match.winner) - expect(@match_to_find.match_scores.first.points).to_not be(1337) + @test = PlayoffStageService.populate_match_below(@match).first + end + + it 'finds the correct match and replaces the match_score' do + expect(@test.teams).to match_array(@match.winner) + expect(@test.match_scores.first.points).to_not be(1337) end end context 'match below has one match_score with the correct team' do - it 'finds the correct match and replaces nothing' do + before do @match_to_find.match_scores = create_list(:match_score, 1, team: @match.winner, points: 42) @match_to_find.save - PlayoffStageService.populate_match_below(@match) - @match_to_find.reload - expect(@match_to_find.teams).to match_array(@match.winner) - expect(@match_to_find.match_scores.first.points).to be(42) + @test = PlayoffStageService.populate_match_below(@match).first + end + + it 'finds the correct match and replaces nothing' do + expect(@test.teams).to match_array(@match.winner) + expect(@test.match_scores.first.points).to be(42) end end context 'match below has two match_scores with the correct teams' do - it 'finds the correct match and replaces nothing' do + before do @companion_match.state = :finished @companion_match.save @match_to_find.match_scores = [create(:match_score, team: @match.winner), create(:match_score, team: @companion_match.winner)] @match_to_find.save - PlayoffStageService.populate_match_below(@match) - @match_to_find.reload - expect(@match_to_find.teams).to match_array([@match.winner, @companion_match.winner]) + @test = PlayoffStageService.populate_match_below(@match).first + end + + it 'finds the correct match and replaces nothing' do + expect(@test.teams).to match_array([@match.winner, @companion_match.winner]) end end end