我有lambdas使用boto3.client()
连接到dynamoDB。
我试着这样测试
@mock.patch("boto3.client")
def test(self, mock_client, test):
handler(event, context)
print(mock_client.call_count) # 1
print(mock_client.put_item.call_count) # 0
但是,mock_client。Call_count为1,但不是put_item_call_count
我的处理程序是这样的:
def handler(event, context):
dynamodb = boto3.client('dynamodb')
response = dynamodb.put_item(// same attributed)
有什么建议,如何测试是否正确的项目被放入数据库而不使用moto?
我相信你已经很接近了,只是有一个小问题。
当你嘲笑boto3。客户端被调用,它返回另一个模拟,您想要评估模拟call_count
。通过访问原始模拟的return_value
,您可以访问创建的魔术模拟。
@mock.patch("boto3.client")
def test(self, mock_client, test):
handler(event, context)
print(mock_client.call_count)
# .return_value refers to the magic mock that's
# created when boto3.client is called
print(mock_client.return_value.put_item.call_count)
您当前评估的是boto3.client.put_item
的调用计数,而不是boto3.client("dynamodb").put_item()
。