Improve spec speed by minimizing database writes
This commit is contained in:
parent
aeb3e64efc
commit
5823ae9952
@ -1,5 +1,5 @@
|
|||||||
RSpec.describe Play, type: :model do
|
RSpec.describe Play, type: :model do
|
||||||
before(:each) do
|
before(:all) do
|
||||||
@game = FactoryGirl.create(:game)
|
@game = FactoryGirl.create(:game)
|
||||||
@user1 = FactoryGirl.create(:user)
|
@user1 = FactoryGirl.create(:user)
|
||||||
@user2 = FactoryGirl.create(:user)
|
@user2 = FactoryGirl.create(:user)
|
||||||
@ -12,6 +12,13 @@ RSpec.describe Play, type: :model do
|
|||||||
@play.save
|
@play.save
|
||||||
end
|
end
|
||||||
|
|
||||||
|
after(:each) do
|
||||||
|
@play.errors.messages.clear
|
||||||
|
@play.hand = []
|
||||||
|
@game.play_to_beat = nil
|
||||||
|
@play.reload
|
||||||
|
end
|
||||||
|
|
||||||
subject { @play }
|
subject { @play }
|
||||||
|
|
||||||
it { should respond_to(:game_id) }
|
it { should respond_to(:game_id) }
|
||||||
@ -58,7 +65,7 @@ RSpec.describe Play, type: :model do
|
|||||||
# Play the lowest card
|
# Play the lowest card
|
||||||
@cards_to_play = @game.current_player.player_cards.order(:value).first
|
@cards_to_play = @game.current_player.player_cards.order(:value).first
|
||||||
@play.player_cards << @cards_to_play
|
@play.player_cards << @cards_to_play
|
||||||
@play.save
|
#@play.save
|
||||||
expect(@play.player_cards.include? @game.lowest_card).to eq true
|
expect(@play.player_cards.include? @game.lowest_card).to eq true
|
||||||
expect(subject.valid?).to eq(true)
|
expect(subject.valid?).to eq(true)
|
||||||
end
|
end
|
||||||
@ -71,13 +78,13 @@ RSpec.describe Play, type: :model do
|
|||||||
@play_to_beat.game = @game
|
@play_to_beat.game = @game
|
||||||
@play_to_beat.player_cards << @play_to_beat.player.player_cards.order(:value).last
|
@play_to_beat.player_cards << @play_to_beat.player.player_cards.order(:value).last
|
||||||
@play_to_beat.save
|
@play_to_beat.save
|
||||||
@play.game.reload
|
|
||||||
|
|
||||||
expect(@play.game.play_to_beat.play).to eq(@play_to_beat)
|
expect(@play.game.play_to_beat.play).to eq(@play_to_beat)
|
||||||
|
|
||||||
# Losing card
|
# Losing card
|
||||||
@cards_to_play = @play.player.player_cards.order(:value).first
|
@cards_to_play = @play.player.player_cards.order(:value).first
|
||||||
@play.player_cards << @cards_to_play
|
@play.player_cards << @cards_to_play
|
||||||
@play.save
|
|
||||||
expect(@game.player_to_beat).to_not eq(@play.player)
|
expect(@game.player_to_beat).to_not eq(@play.player)
|
||||||
expect(@play.hand_valid?).to eq(false)
|
expect(@play.hand_valid?).to eq(false)
|
||||||
expect(@play.errors[:player_cards].include?('invalid hand')).to eq(true)
|
expect(@play.errors[:player_cards].include?('invalid hand')).to eq(true)
|
||||||
@ -91,10 +98,10 @@ RSpec.describe Play, type: :model do
|
|||||||
]
|
]
|
||||||
|
|
||||||
@play_to_beat = Play.new
|
@play_to_beat = Play.new
|
||||||
@play_to_beat.player = @game.players.first
|
@play_to_beat.player = @game.players.where.not(id: @game.active_player.id).first
|
||||||
@play_to_beat.player_cards << @cards_to_beat
|
@play_to_beat.player_cards << @cards_to_beat
|
||||||
@play_to_beat.game = @game
|
@play_to_beat.game = @game
|
||||||
@play_to_beat.save
|
#@play_to_beat.save
|
||||||
|
|
||||||
@play.player_cards = [
|
@play.player_cards = [
|
||||||
@spade4 = PlayerCard.new(@deck.cards[4].instance_values),
|
@spade4 = PlayerCard.new(@deck.cards[4].instance_values),
|
||||||
@ -104,11 +111,15 @@ RSpec.describe Play, type: :model do
|
|||||||
# Shared tests
|
# Shared tests
|
||||||
[@play_to_beat,@play].each do |play|
|
[@play_to_beat,@play].each do |play|
|
||||||
expect(play.errors[:player_cards]).to eq([])
|
expect(play.errors[:player_cards]).to eq([])
|
||||||
expect(play.valid?).to eq(true)
|
|
||||||
expect(play.player_cards.count).to eq(2)
|
|
||||||
expect(play.hand_type).to eq('double')
|
expect(play.hand_type).to eq('double')
|
||||||
|
expect(play.player_cards.length).to eq(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@play.reload
|
||||||
|
@game.reload
|
||||||
|
binding.pry unless !@play.valid?
|
||||||
|
|
||||||
|
expect(@play.valid?).to eq(false)
|
||||||
expect(@play.beats? @play_to_beat).to eq(false)
|
expect(@play.beats? @play_to_beat).to eq(false)
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -118,14 +129,14 @@ RSpec.describe Play, type: :model do
|
|||||||
context 'it beats the hand_to_beat' do
|
context 'it beats the hand_to_beat' do
|
||||||
it 'single' do
|
it 'single' do
|
||||||
@play_to_beat = Play.new
|
@play_to_beat = Play.new
|
||||||
@play_to_beat.player = @game.players.last
|
@play_to_beat.player = @game.players.where.not(id: @game.current_player.id).first
|
||||||
@play_to_beat.game = @game
|
@play_to_beat.game = @game
|
||||||
@play_to_beat.player_cards << @play_to_beat.player.player_cards.order(:value).first
|
@play_to_beat.player_cards << @play_to_beat.player.player_cards.order(:value).first
|
||||||
@play_to_beat.save
|
@play_to_beat.save
|
||||||
|
|
||||||
@cards_to_play = @play.player.player_cards.order(:value).last
|
@cards_to_play = @play.player.player_cards.order(:value).last
|
||||||
@play.player_cards << @cards_to_play
|
@play.player_cards << @cards_to_play
|
||||||
@play.save
|
|
||||||
expect(@play.valid?).to eq(true)
|
expect(@play.valid?).to eq(true)
|
||||||
expect(@play.errors[:player_cards]).to eq([])
|
expect(@play.errors[:player_cards]).to eq([])
|
||||||
end
|
end
|
||||||
@ -165,7 +176,7 @@ RSpec.describe Play, type: :model do
|
|||||||
# Play the hand
|
# Play the hand
|
||||||
@cards_to_play = @game.current_player.inventory[0]
|
@cards_to_play = @game.current_player.inventory[0]
|
||||||
@play.hand << @cards_to_play
|
@play.hand << @cards_to_play
|
||||||
@play.save
|
|
||||||
expect(@play.hand_valid?).to eq(true)
|
expect(@play.hand_valid?).to eq(true)
|
||||||
@game.reload
|
@game.reload
|
||||||
expect(@game.play_to_beat.play).to eq(@play)
|
expect(@game.play_to_beat.play).to eq(@play)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user