Improve spec speed by minimizing database writes

This commit is contained in:
Jesse C. Fisher 2015-08-17 15:11:45 -07:00
parent aeb3e64efc
commit 5823ae9952

View File

@ -1,5 +1,5 @@
RSpec.describe Play, type: :model do
before(:each) do
before(:all) do
@game = FactoryGirl.create(:game)
@user1 = FactoryGirl.create(:user)
@user2 = FactoryGirl.create(:user)
@ -12,6 +12,13 @@ RSpec.describe Play, type: :model do
@play.save
end
after(:each) do
@play.errors.messages.clear
@play.hand = []
@game.play_to_beat = nil
@play.reload
end
subject { @play }
it { should respond_to(:game_id) }
@ -58,7 +65,7 @@ RSpec.describe Play, type: :model do
# Play the lowest card
@cards_to_play = @game.current_player.player_cards.order(:value).first
@play.player_cards << @cards_to_play
@play.save
#@play.save
expect(@play.player_cards.include? @game.lowest_card).to eq true
expect(subject.valid?).to eq(true)
end
@ -71,13 +78,13 @@ RSpec.describe Play, type: :model do
@play_to_beat.game = @game
@play_to_beat.player_cards << @play_to_beat.player.player_cards.order(:value).last
@play_to_beat.save
@play.game.reload
expect(@play.game.play_to_beat.play).to eq(@play_to_beat)
# Losing card
@cards_to_play = @play.player.player_cards.order(:value).first
@play.player_cards << @cards_to_play
@play.save
expect(@game.player_to_beat).to_not eq(@play.player)
expect(@play.hand_valid?).to eq(false)
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.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.game = @game
@play_to_beat.save
#@play_to_beat.save
@play.player_cards = [
@spade4 = PlayerCard.new(@deck.cards[4].instance_values),
@ -104,11 +111,15 @@ RSpec.describe Play, type: :model do
# Shared tests
[@play_to_beat,@play].each do |play|
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.player_cards.length).to eq(2)
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)
end
@ -118,14 +129,14 @@ RSpec.describe Play, type: :model do
context 'it beats the hand_to_beat' do
it 'single' do
@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.player_cards << @play_to_beat.player.player_cards.order(:value).first
@play_to_beat.save
@cards_to_play = @play.player.player_cards.order(:value).last
@play.player_cards << @cards_to_play
@play.save
expect(@play.valid?).to eq(true)
expect(@play.errors[:player_cards]).to eq([])
end
@ -165,7 +176,7 @@ RSpec.describe Play, type: :model do
# Play the hand
@cards_to_play = @game.current_player.inventory[0]
@play.hand << @cards_to_play
@play.save
expect(@play.hand_valid?).to eq(true)
@game.reload
expect(@game.play_to_beat.play).to eq(@play)