Improve spec speed by minimizing database writes
This commit is contained in:
parent
aeb3e64efc
commit
7600bfaad2
@ -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,18 @@ RSpec.describe Play, type: :model do
|
||||
@play.save
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@play.errors.messages.clear
|
||||
@play.hand = []
|
||||
if @game.play_to_beat
|
||||
@game.play_to_beat.play.hand = []
|
||||
end
|
||||
@game.play_to_beat = nil
|
||||
@game.play_to_beat_id = nil
|
||||
@play.reload
|
||||
@game.reload
|
||||
end
|
||||
|
||||
subject { @play }
|
||||
|
||||
it { should respond_to(:game_id) }
|
||||
@ -72,6 +84,7 @@ RSpec.describe Play, type: :model do
|
||||
@play_to_beat.player_cards << @play_to_beat.player.player_cards.order(:value).last
|
||||
@play_to_beat.save
|
||||
@play.game.reload
|
||||
@play.reload
|
||||
|
||||
expect(@play.game.play_to_beat.play).to eq(@play_to_beat)
|
||||
# Losing card
|
||||
@ -91,7 +104,7 @@ 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
|
||||
@ -104,11 +117,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.count).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,7 +135,7 @@ 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
|
||||
@ -126,6 +143,8 @@ RSpec.describe Play, type: :model do
|
||||
@cards_to_play = @play.player.player_cards.order(:value).last
|
||||
@play.player_cards << @cards_to_play
|
||||
@play.save
|
||||
@play.reload
|
||||
binding.pry unless @play.valid?
|
||||
expect(@play.valid?).to eq(true)
|
||||
expect(@play.errors[:player_cards]).to eq([])
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user