Fix player count game validation

This commit is contained in:
Jesse C. Fisher 2021-07-08 23:47:37 -07:00
parent ef17b1f8f9
commit 2134ea02ce
2 changed files with 5 additions and 4 deletions

View File

@ -50,10 +50,10 @@ class Game < ActiveRecord::Base
def validate_startable
@player_count = players.count
@user_count = players.where.not(soft_token: nil).count
errors[:players] = 'Must be at least 2 players.' if @user_count < 2
errors[:players] = 'Must be less than 5 players.' if @user_count > 4
errors[:status] = 'Game already started...' if self.status.nil? == false
errors[:player_cards] = 'Cards already dealt...' unless player_cards.empty?
errors.add(:players, 'Must be at least 2 players.') if @player_count < 2
errors.add(:players, 'Must be less than 5 players.') if @player_count > 4
errors.add(:status, 'Game already started...') if self.status.nil? == false
errors.add(:player_cards, 'Cards already dealt...') unless player_cards.empty?
fail StandardError, errors if errors.count > 0
return true
end

View File

@ -67,6 +67,7 @@ RSpec.describe Game, type: :model do
@game.players.new(user_id: User.last.id)
expect { @game.validate_startable }.to raise_error StandardError
expect(@game.startable?).to eq(false)
binding.pry
expect(@game.errors[:players])
.to include 'Must be less than 5 players.'
end