我正在尝试使用存根(stub)在pytest中测试DynamoDB put_item并得到错误,并且不确定在add_response中将期望什么。
def test_put_item_dynamodb(dynamodb_resource_stub, item_fake, condition_expression_fake, return_values_fake,
return_consumed_capacity_fake, return_item_metrics_fake):
expected_params = {
'Item': item_fake,
'Condition_Expression': condition_expression_fake,
'Return_Values': return_values_fake,
'Return_Consumed_Capacity': return_consumed_capacity_fake,
'return_item_collection_metrics': return_item_metrics_fake
}
expected_response = {
'ResponseMetadata': {
'RequestId': 'ABCD1234EFGH5678',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'server': 'Server', 'date': 'Mon, 01 Jan 2019 12:12:12 GMT',
'content-type': 'application/x-amz-json-1.0',
'content-length': '2',
'connection': 'keep-alive',
'x-amzn-requestid': 'ABCD1234EFGH5678',
'x-amz-crc32': '12456789'
},
'RetryAttempts': 0
}
}
input_response = deepcopy(expected_response)
dynamodb_resource_stub.add_response(method='put_item', service_response=input_response,
expected_params=expected_params)
dynamodb_resource_stub.activate()
actual_response = src.lambda_handler.put_item_dynamodb_table(item=[item_fake],
condition_expression=condition_expression_fake,
return_values=return_values_fake,
return_consumed_capacity=return_consumed_capacity_fake,
return_item_collection_metrics=return_item_metrics_fake)
assert expected_response == actual_response
dynamodb_resource_stub.assert_no_pending_responses()
这是错误-
TypeError: Unsupported type "<class 'function'>" for value "<function aws_request_id_fake at 0x1073776a8>"
aws_request_id -它是Item的一部分,当在上面的错误中调用item_fake时,似乎在测试功能中未正确调用aws_request_id_fake。
@fixture(scope='module')
def aws_request_id_fake(request):
return 'xyz789'
@fixture(scope='module')
def item_fake(request):
return {
'aws_request_id': aws_request_id_fake,
'messageId': messageid_fake
}