def test_calls_throw_on_game_with_integer_of_number_of_pins_entered(self, input_mock): input_mock.return_value = "10" game = mock.Mock(spec_set=BowlingGame) lane = BowlingLane() lane._get_throw(game) game.throw.assert_called_once_with(10)
def test_breaks_from_add_bowler_loop_when_no_name_entered(self, add_bowler, input_mock): name_list = ['aaron', 'dave', ''] def side_effect(*args): return name_list.pop(0) if name_list else '' input_mock.side_effect=side_effect lane = BowlingLane() lane.prompt_for_bowlers() self.assertEqual(3, input_mock.call_count) self.assertEqual([ (('aaron',), {}), (('dave',), {}), ], add_bowler.call_args_list)
def test_prompts_for_user_throw(self, input_mock): input_mock.return_value = "0" lane = BowlingLane() lane._get_throw(mock.Mock(spec_set=BowlingGame)) input_mock.assert_called_with(THROW_BALL_PROMPT)
def test_does_not_call_add_bowler_when_no_input_value(self, add_bowler, input_mock): input_mock.return_value = '' lane = BowlingLane() lane.prompt_for_bowlers() self.assertFalse(add_bowler.called)
def test_calls_add_bowler_4_times_with_valid_raw_input_value(self, add_bowler, input_mock): lane = BowlingLane() lane.prompt_for_bowlers() add_bowler.assert_called_with(input_mock.return_value) self.assertEqual(4, add_bowler.call_count)
def test_prompts_for_name_when_prompt_for_bowlers_called(self, input_mock): lane = BowlingLane() lane.prompt_for_bowlers() input_mock.assert_called_with(ADD_BOWLER_PROMPT)
def setUp(self): self.lane = BowlingLane()
class BowlingLaneTests(TestCase): def setUp(self): self.lane = BowlingLane() @mock.patch('__builtin__.raw_input') def test_prompts_for_name_when_prompt_for_bowlers_called(self, input_mock): lane = BowlingLane() lane.prompt_for_bowlers() input_mock.assert_called_with(ADD_BOWLER_PROMPT) @mock.patch('__builtin__.raw_input') @mock.patch('bowling.game.BowlingLane.add_bowler') def test_calls_add_bowler_4_times_with_valid_raw_input_value(self, add_bowler, input_mock): lane = BowlingLane() lane.prompt_for_bowlers() add_bowler.assert_called_with(input_mock.return_value) self.assertEqual(4, add_bowler.call_count) @mock.patch('__builtin__.raw_input') @mock.patch('bowling.game.BowlingLane.add_bowler') def test_does_not_call_add_bowler_when_no_input_value(self, add_bowler, input_mock): input_mock.return_value = '' lane = BowlingLane() lane.prompt_for_bowlers() self.assertFalse(add_bowler.called) @mock.patch('__builtin__.raw_input') @mock.patch('bowling.game.BowlingLane.add_bowler') def test_breaks_from_add_bowler_loop_when_no_name_entered(self, add_bowler, input_mock): name_list = ['aaron', 'dave', ''] def side_effect(*args): return name_list.pop(0) if name_list else '' input_mock.side_effect=side_effect lane = BowlingLane() lane.prompt_for_bowlers() self.assertEqual(3, input_mock.call_count) self.assertEqual([ (('aaron',), {}), (('dave',), {}), ], add_bowler.call_args_list) @mock.patch('bowling.game.BowlingGame') def test_player_is_added_to_bowlers(self, game_class): self.lane.add_bowler('dave') self.assertEqual([['dave', game_class.return_value]], self.lane._bowlers) game_class.assert_called_once_with() @mock.patch('bowling.game.BowlingGame') def test_multiple_players_are_added_to_bowlers(self, game_class): dave_game = mock.Mock(spec_set=BowlingGame) aaron_game = mock.Mock(spec_set=BowlingGame) game_list = [dave_game, aaron_game] def side_effect(*args): return game_list.pop(0) game_class.side_effect = side_effect self.lane.add_bowler('dave') self.lane.add_bowler('aaron') self.assertEqual([ ['dave', dave_game], ['aaron', aaron_game] ], self.lane._bowlers) @mock.patch('bowling.game.BowlingLane._get_all_bowler_throws') def test_play_game_calls_get_all_bowler_throws_for_1_thru_10(self, get_all_throws): self.lane.play_game() self.assertEqual([ ((1, ),{}), ((2, ),{}), ((3, ),{}), ((4, ),{}), ((5, ),{}), ((6, ),{}), ((7, ),{}), ((8, ),{}), ((9, ),{}), ((10, ),{}), ], get_all_throws.call_args_list) @mock.patch('bowling.game.BowlingLane._get_user_throws') def test_get_user_throws_for_each_bowler(self, get_user_throws): game_1 = mock.Mock(spec_set=BowlingGame) game_2 = mock.Mock(spec_set=BowlingGame) self.lane._bowlers = [ ['bowler_one', game_1], ['bowler_two', game_2], ] self.lane._get_all_bowler_throws(1) self.assertEqual([ ((game_1, ),{}), ((game_2, ),{}), ], get_user_throws.call_args_list) @mock.patch('__builtin__.raw_input') def test_prompts_for_user_throw(self, input_mock): input_mock.return_value = "0" lane = BowlingLane() lane._get_throw(mock.Mock(spec_set=BowlingGame)) input_mock.assert_called_with(THROW_BALL_PROMPT) @mock.patch('__builtin__.raw_input') def test_calls_throw_on_game_with_integer_of_number_of_pins_entered(self, input_mock): input_mock.return_value = "10" game = mock.Mock(spec_set=BowlingGame) lane = BowlingLane() lane._get_throw(game) game.throw.assert_called_once_with(10) @mock.patch('bowling.game.BowlingLane._get_throw') def test_gets_user_throw_only_once_when_game_is_complete(self, get_throw): game = mock.Mock(spec_set=BowlingGame) game.active_frame.is_complete.return_value = True self.lane._get_user_throws(game) get_throw.assert_called_once_with(game) @mock.patch('bowling.game.BowlingLane._get_throw') def test_get_throw_called_utill_frame_is_complete(self, get_throw): is_complete_values = [False, True] def side_effect(*args): return is_complete_values.pop(0) game = mock.Mock(spec_set=BowlingGame) game.active_frame.is_complete.side_effect = side_effect self.lane._get_user_throws(game) self.assertEqual([ ((game,), {}), ((game,), {}), ], get_throw.call_args_list) @mock.patch('bowling.game.BowlingLane.get_formatted_score') def test_display_results_saves_bowling_game_for_bowler(self, get_formatted_score): get_formatted_score.return_value = "123" self.lane._bowlers = [('eric', BowlingGame())] self.lane.display_results() saved_score = models.BowlingScore.objects.get(pk=1) self.assertEqual("eric", saved_score.name) self.assertEqual("123", saved_score.score)