我正在尝试按照文档执行此操作:@pytest.fixture()def aws_credentials(): """Mocked AWS Credentials for moto.""" os.environ["AWS_ACCESS_KEY_ID"] = "testing" os.environ["AWS_SECRET_ACCESS_KEY"] = "testing" os.environ["AWS_SECURITY_TOKEN"] = "testing" os.environ["AWS_SESSION_TOKEN"] = "testing"@pytest.fixture()def sts(aws_credentials): with mock_sts(): yield boto3.client("sts", region_name="us-east-1")@pytest.fixturedef sns(aws_credentials): with mock_sns(): yield boto3.resource("sns", region_name="us-east-1")def test_publish(sns): resp = sns.create_topic(Name="sdfsdfsdfsd")我收到错误: def test_publish(sns):> topic_arn = sns.create_topic(Name="sdfsdfsdfsd")E AttributeError: 'generator' object has no attribute 'create_topic'
1 回答
繁花如伊
TA贡献2012条经验 获得超12个赞
好吧,我不是100%确定为什么,但添加sts装饰器似乎已经解决了这个问题:
@mock_sts
def test_publish(sns):
resp = sns.create_topic(Name="sdfsdfsdfsd")
我从这篇文章中得到了这一点,但我仍然不清楚它是如何工作的:https://www.serverlessops.io/blog/aws-lambda-serverless-development-workflow-part2-testing-debugging
这是因为boto需要使用sts,所以我也需要嘲笑它吗?我使用带有配置文件的凭证文件从笔记本电脑访问 AWS
编辑
您还必须使用 yield 来返回客户端。在这里使用返回给了我一个sts错误。我也想更好地理解这一点。我假设我需要使用收益率,因为它是一个生成器?
@pytest.fixture
def sns(aws_credentials):
with mock_sns():
# using return here causes below error
return boto3.resource("sns", region_name="us-east-1")
不使用产能时出错:
botocore.exceptions.ClientError:调用 CreateTopic 操作时发生错误(InvalidClientTokenId):请求中包含的安全令牌无效
添加回答
举报
0/150
提交
取消