def test_create(self): with self.assertRaises(NotImplementedError) as context: Listener(recogniser=self.mock_recogniser, microphone=self.mock_microphone) self.assertIn( 'No available speech to text recognition service chosen.', str(context.exception))
class TestListenerWithSphinx(TestCase): def setUp(self): self.mock_recogniser = Mock(autospec='sr.Recognizer') self.mock_microphone = MagicMock(autospec='sr.Microphone') self.listener = Listener(service='sphinx', recogniser=self.mock_recogniser, microphone=self.mock_microphone) def test_create(self): self.assertIsInstance(self.listener, Listener) def test_listen(self): self.listener.listen() # __enter__ is needed due to logic in listener class being inside a python 'with' block # when you use 'with', this calls a magic method of __enter__ passing result to 'as' self.mock_recogniser.assert_has_calls([ # Need to add if adjusting the main listener for ambient noise # call.adjust_for_ambient_noise(self.mock_microphone.__enter__()), call.listen(self.mock_microphone.__enter__()) ]) def test_listen_raises_value_error(self): with patch.object(self.listener, 'transcribe') as transcribe: transcribe.side_effect = sr.UnknownValueError() with self.assertRaises(ValueError) as context: self.listener.listen() self.assertIn('Could not translate speech', str(context.exception)) def test_transcribe(self): audio = lambda: None self.listener.transcribe(audio) self.mock_recogniser.recognize_sphinx.assert_called_once_with(audio)
def __init__(self, listener=None, speaker=None): self.__listener = listener or Listener( service='sphinx') # sphinx | google self.__speaker = speaker or Speaker(service='say') # say | espeak
def setUp(self): self.mock_recogniser = Mock(autospec='sr.Recognizer') self.mock_microphone = MagicMock(autospec='sr.Microphone') self.listener = Listener(service='sphinx', recogniser=self.mock_recogniser, microphone=self.mock_microphone)