Game start spec

This commit is contained in:
Jesse C. Fisher 2015-04-09 17:17:51 -07:00
parent 6d80ff29a6
commit 39adcdb9e4
2 changed files with 16 additions and 12 deletions

View File

@ -13,15 +13,15 @@ class Game < ActiveRecord::Base
end end
def validate_startable def validate_startable
@player_count = players.count @player_count = players.size
errors[:notice] = 'Must be at least 2 players.' if @player_count < 2 errors[:players] = 'Must be at least 2 players.' if @player_count < 2
errors[:players] = 'Must be less than 5 players.' if @player_count > 4 errors[:players] = 'Must be less than 5 players.' if @player_count > 4
end end
def startable? def startable?
@player_count = players.size @player_count = players.size
bool = @player_count.between?(2, 4) bool = @player_count.between?(2, 4)
bool = status != 'Game Started' #bool = status != 'Game Started'
end end
def add_player_from_user(user) def add_player_from_user(user)

View File

@ -14,26 +14,30 @@ RSpec.describe Game, type: :model do
describe 'validations' do describe 'validations' do
describe 'player_count' do describe 'player_count' do
context 'when there are between 2 and 4 players' do context 'when there are between 2 and 4 players' do
it 'is valid' do it 'is startable' do
@game.players.new @game.players.new
@game.players.new @game.players.new
expect(@game).to be_valid expect(@game.players.size).to eq(2)
expect(@game.startable?).to eq(true)
end end
end end
context 'when there are less than 2 players' do context 'when there are less than 2 players' do
it 'fails validation with too few players' do it 'is not startable with too few players' do
expect(@game).to_not be_valid @game.validate_startable
expect(@game.players.size).to eq(0)
expect(@game.startable?).to eq(false)
expect(@game.errors[:players]) expect(@game.errors[:players])
.to include 'Must be at least 2 players.' .to include 'Must be at least 2 players.'
end end
end end
context 'when there are more than 4 players' do context 'when there are more than 4 players' do
it 'fails validation with too many players' do it 'fails validation with too many players' do
5.times do 6.times do
@game.players.new @game.players.new
end end
expect(@game).to_not be_valid @game.validate_startable
expect(@game.startable?).to eq(false)
expect(@game.errors[:players]) expect(@game.errors[:players])
.to include 'Must be less than 5 players.' .to include 'Must be less than 5 players.'
end end
@ -41,7 +45,7 @@ RSpec.describe Game, type: :model do
end end
end end
it 'Can not be started if game is invalid' do #it 'Can not be started if game is invalid' do
expect(@game.start).to raise_error #expect(@game.start).to raise_error
end #end
end end