我正试图用Moto模拟SNS,使用pytest文档中的示例。
sns.create_topic((有效,但sns.publish((无效。从boto文档中,我应该能够调用publish((,如下所示:
@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.fixture
def sns(aws_credentials):
with mock_sns():
yield boto3.resource("sns", region_name="us-east-1")
@mock_sts
def test_publish(sns):
resp = sns.create_topic(Name="sdfsdfsdfsd")
mesg = {"TopicArn": "arnsdfsdf", "Message": "sdfsdfsdfsd"}
response = sns.publish(mesg)
我得到以下错误:
AttributeError:"sns.ServiceResource"对象没有属性"publish">
Moto不支持发布吗?我希望它是有效的,对publish((的调用对我来说是有效的——我不想让它发布。
SNS使用boto3客户端,而不是资源。所以改变这个:
with mock_sns():
yield boto3.resource("sns", region_name="us-east-1")
到此:
with mock_sns():
yield boto3.client("sns", region_name="us-east-1")
它应该起作用。
示例测试用例:https://github.com/spulec/moto/blob/master/tests/testrongns/test_publishing_boto3.py#L28