多次调用短截线导致异常

2024-10-04 05:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用短截线嘲笑kmsclient。我使用的代码是

with botocore.stub.Stubber(s3) as stubber:
        with botocore.stub.Stubber(kms) as stubber2:
            stubber.add_response('copy_object', response, expectedParams)
            stubber.activate()
            stubber2.add_response('decrypt', response2, expectedParams2)
            stubber2.activate()
            handleCore(__makeValidEvent(), None, s3, kms)
            stubber.assert_no_pending_responses()
            stubber2.assert_no_pending_responses()

在实际实现中,kmsclient调用发生两次,这将导致以下异常

^{pr2}$

有人能告诉我如何在同一个对象上进行多个调用(本例中是kmsclient)


Tags: noadds3responseaswithassertactivate
1条回答
网友
1楼 · 发布于 2024-10-04 05:29:49

我通过为同一方法添加多个响应来解决这个问题。在

测试代码示例:

############## Test Code
client = botocore.session.get_session().create_client("cloudformation", region_name="us-east-1")
stubber = Stubber(client)

# Generate multiple responses to be returned by boto
responses = [
    {
        "StackId": "stack-a",
    },
    {
        "StackId": "stack-b",
    },
    {
        "StackId": "stack-c",
    },
]

# Add each response to stubber for the same method - "update_termination_protection"
for response in responses:
    stubber.add_response(
        "update_termination_protection",
        response,
    )
stubber.activate()
actual = method_to_test(client, data)
stubber.deactivate()
assert actual == True

测试方法示例:

^{pr2}$

这是有效的,没有异常。在

相关问题 更多 >