diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 427ae16..d8eac17 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -22,6 +22,7 @@ class GamesController < ApplicationController def play_hand # TODO validate player's turn, current_user == @game.current_player.user + # TODO move this logic out of controller maybe to model(s) @cards_to_play = @game.current_player.player_cards.find(params[:player_card_ids]) # Instantiate the play diff --git a/spec/features/plays_spec.rb b/spec/features/plays_spec.rb new file mode 100644 index 0000000..b754561 --- /dev/null +++ b/spec/features/plays_spec.rb @@ -0,0 +1,86 @@ +require 'rails_helper' +include Warden::Test::Helpers +Warden.test_mode! + +feature "Play a hand", :type => :feature do + + after(:each) do + Warden.test_reset! + end + + # Scenario: The first play must be the lowest dealt card + # Given: A new game and I am the holder of the lowest card + # When I play a hand containing the lowest card + # Then I see a successful play message + scenario 'first play contains lowest card' do + # TODO extract game setup to a factory + game = FactoryGirl.create :game + user1 = FactoryGirl.create :user + user2 = FactoryGirl.create :user + game.add_player_from_user user1 + game.add_player_from_user user2 + game.start + login_as(game.current_player.user, :scope => :user) + visit game_path game + # Click the lowest card checkbox + @card_to_play = game.lowest_card + check @card_to_play.to_s + click_button "Play Hand" + expect(page).to have_content("Played " + game.lowest_card.to_s) + # create game with players + # current_player plays hand containing lowest card + # expect successful play message + end + + scenario 'first play does not contain lowest card' do + game = FactoryGirl.create :game + user1 = FactoryGirl.create :user + user2 = FactoryGirl.create :user + game.add_player_from_user user1 + game.add_player_from_user user2 + game.start + login_as(game.current_player.user, :scope => :user) + visit game_path game + # Click the lowest card checkbox + @card_to_play = game.current_player.player_cards.order(:value).last + check @card_to_play.to_s + click_button "Play Hand" + expect(page).to have_content("First hand must contain the lowest card: #{game.lowest_card.to_s}") + # current_player plays hand not containing lowest card + # expect unsuccessful play message hand MUST contain lowest card + end + + # Scenario: Player can play a valid hand on their turn + # Given I am the active player + # When I play a valid hand + # Then I see a successful play message + scenario 'active player can play a valid hand' do + # create game with players + # current_player plays a valid hand + # expect successful play message + pending "to be implemented" + end + + # Scenario: Player can not play an invalid hand on their turn + # Given I am the active player + # When I play an invalid hand + # Then I see an invalid hand message + scenario 'active player can play a valid hand' do + # create game with players + # current_player plays an invalid hand + # expect invalid hand message + pending "to be implemented" + end + + # Scenario: Player can not play a hand when it is not their turn + # Given I am not the active player + # When I play a hand + # Then I see an unsuccessful play message + scenario 'inactive player can not play a hand' do + # create game with players + # not_current_player plays a valid hand + # expect unsuccessful play message + pending "to be implemented" + end + +end diff --git a/spec/models/play_spec.rb b/spec/models/play_spec.rb index 816daf2..cfd8f78 100644 --- a/spec/models/play_spec.rb +++ b/spec/models/play_spec.rb @@ -1,5 +1,39 @@ require 'rails_helper' RSpec.describe Play, :type => :model do - pending "add some examples to (or delete) #{__FILE__}" + before(:each) do + @game = FactoryGirl.create(:game) + @user1 = FactoryGirl.create(:user) + @user2 = FactoryGirl.create(:user) + @game.add_player_from_user @user1 + @game.add_player_from_user @user2 + @game.save + @game.start + @play = @game.current_player.plays.new + @play.game_id = @game.id + @play.save + end + + subject { @play } + + it { should respond_to(:game_id) } + + it '#game_id returns an integer' do + expect(@play.game_id).to match @game.id + end + + describe 'validations' do + describe 'first_play' do + context 'when there have been no plays yet' do + it 'is valid if it contains the lowest card' do + expect(@game.plays.count).to eq(1) + expect(@game.current_player).to eq(@game.lowest_card.player) + @card_to_play = @game.current_player.player_cards.order(:value).first + @card_to_play.play_id = @play.id + @card_to_play.save + expect(@play.player_cards.include? @game.lowest_card).to eq true + end + end + end + end end