I a learning python mock, and having problem to check if a method on a mocked object has been called.
The code I want to test is this:
def main(flags, directory_service):
if flags.has_schema:
directory_service.HasSchema(SCHEMA_NAME)
sys.exit(1)
My test code is:
def testHaltsAfterCheckingSchema(self):
mock_flags = self._CreateDefaultFlags()
mock_flags.has_schema = True
mock_directory_service = MagicMock()
with self.assertRaises(SystemExit) as cm:
key_distributor.main(mock_flags, mock_directory_service)
self.assertTrue(mock_directory_service.HasSchema.assert_called_with(key_distributor.SCHEMA_NAME))
self.assertEqual(cm.exception.code, 1)
And when I run it, I get this exception:
FAIL: testHaltsAfterCheckingSchema (test123.Test123)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test123.py", line 34, in testHaltsAfterCheckingSchema
self.assertTrue(mock_directory_service.HasSchema.assert_called_with(key_distributor.SCHEMA_NAME))
AssertionError: None is not true
Aucun commentaire:
Enregistrer un commentaire