'Mocking and assert_called_once_with()

I want to assert_called_once_with for the mocked object, here is what I do:

log_service.py

def insert_log(self, log):
  session = boto3.Session(profile_name='test')
  dynamodb = session.resource('dynamodb')
  table = dynanodb.Table('log_info')
  table.put_item(Item=log.__dict__)

test_log_service.py

class Test(Unittest.TestCase):
  def setUp(self):
    self.log_service = LogService()

  @patch("boto3.Session")
  def test_insert_log(self, mock_session):
    log = LogModel()
    mock_dynamodb = MagicMock()
    mock_table = MagicMock()
    mock_session.resource = mock_dynamodb
    mock_dynamodb.Table = mock_table
    self.log_service.insert_log(log)
    mock_table.put_item.assert_called_once_with(Item=log)

I expected that it should true, however, it always return:

AssertionError: Expected 'put_item' to be called once. Called 0 times.

Could someone help me to point out the problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source