def testCommandRecognized(self): from parser import Parser commandWords = MagicMock() commandWords.isCommand = MagicMock(return_value=True) p = Parser(commandWords) result = p._commandRecognized("valid command") errorMsg = "Expected Parser._commandRecognized() to return True." self.assertTrue(result, errorMsg)
def testGetNextCommand(self): from parser import Parser commandWords = MagicMock() p = Parser(commandWords) #Create mock objects #raw_input = MagicMock(side_effect=["unrecognized command", "good command"]) p._commandRecognized = MagicMock(side_effect=[False, True]) fakeCommand = MagicMock() p._commandWords.getCommand = MagicMock(return_value=fakeCommand) #Patch raw_input and call getNextCommand() rawInputMock = MagicMock(side_effect=["unrecognized cmd", "valid cmd"]) with patch('parser.raw_input', create=True, new=rawInputMock): command = p.getNextCommand() #Assert calls made errorMsg = "Expected raw_input to be called twice." self.assertEqual(rawInputMock.call_count, 2, errorMsg) errorMsg = "Expected Parser._commandRecognized() to be called twice." self.assertEqual(p._commandRecognized.call_count, 2, errorMsg) errorMsg = "Parser.getNextCommand() did not respond expected command." self.assertEqual(command, fakeCommand, errorMsg)