使用 moto 中的 botocore.stub 和 mock_s3 我可以像下面那样对 boto3 S3 客户端进行存根,
from moto import mock_s3
from botocore.stub import Stubber
class TestS3(unittest.TestCase):
@mock_s3
def setUp(self):
self.s3_client = boto3.client('s3', region_name='us-west-1')
self.stubber = Stubber(client=self.s3_client)
然后在测试函数中,我可以像下面一样使用 add_client_error 和 add_response 并根据需要添加断言,
@mock_s3
def test_put_object_exception(self):
self.stubber.add_client_error('put_object', service_error_code='500')
with self.stubber:
self.s3_service.put_object(data='/some/dummy/path', key='/some/dummy/key')
但是在我的 S3 类中,我使用 S3Transfer upload_file 将文件上传到 S3,有没有办法模拟 S3Transfer(self.s3_client).upload_file 方法?