我想在cloudformation客户端引发AlreadyExistsException时测试我的代码行为。但是我不知道如何用python模拟来提高它。
class StackHelper:
def __init__(self, stack_name: str, cfn_client, capabilities: List[str]):
self.stack_name = stack_name
self.cfn_client = cfn_client
self.capabilities = capabilities
def deploy(self, template_url: str, params: object = object()):
try:
self.create_stack(template_url, params)
except self.cfn_client.exceptions.AlreadyExistsException as e:
try:
print('Stack already exists => update_stack')
self.update_stack(template_url, params)
except ClientError as e:
if e.response['Error']['Message'] == 'No updates are to be performed.':
print('Nothing to update.')
else:
raise
测试类似于:
mock_cfn_client = Mock()
mock_cfn_client.return_value.create_stack.side_effect = Mock(side_effect=???)
为此,我已经嘲笑了cfn_client
,但是现在却在如何引发我期望在except self.cfn_client.exceptions.AlreadyExistsException
的代码中遇到的boto3异常方面苦苦挣扎。