python - 使用 moto 模拟 SNS 无法正常工作
问题描述
在我的单元测试中:
def test_my_function_that_publishes_to_sns():
conn = boto3.client("sns", region_name="us-east-1")
mock_topic = conn.create_topic(Name="mock-topic")
topic_arn = mock_topic.get("TopicArn")
os.environ["SNS_TOPIC"] = topic_arn
# call my_function
my_module.my_method()
正在测试的功能
# inside my_module, my_function...
sns_client.publish(
TopicArn=os.environ["SNS_TOPIC"], Message="my message",
)
我得到错误:botocore.errorfactory.NotFoundException: An error occurred (NotFound) when calling the Publish operation: Endpoint with arn arn:aws:sns:us-east-1:123456789012:mock-topic not found
没有意义,这就是 moto 应该创建和嘲笑的主题。为什么说它不存在?如果我 conn.publish(TopicArn=topic_arn, Message="sdfsdsdf")
在单元测试本身内部调用它似乎是在模拟它,但它不会为单元测试执行的 my_module.my_method() 模拟它。也许它过早地破坏了嘲笑的话题?
编辑我尝试了各种方式,我得到了完全相同的错误:
# Using context manager
def test_my_function_that_publishes_to_sns():
with mock_sns():
conn = boto3.client("sns", region_name="us-east-1")
mock_topic = conn.create_topic(Name="mocktopic")
topic_arn = mock_topic.get("TopicArn")
os.environ["SNS_TOPIC"] = topic_arn
# call my_function
my_module.my_method()
# Using decorator
@mock_sns
def test_my_function_that_publishes_to_sns():
conn = boto3.client("sns", region_name="us-east-1")
mock_topic = conn.create_topic(Name="mocktopic")
topic_arn = mock_topic.get("TopicArn")
os.environ["SNS_TOPIC"] = topic_arn
# call my_function
my_module.my_method()
# Using decorator and context manager
@mock_sns
def test_my_function_that_publishes_to_sns():
with mock_sns():
conn = boto3.client("sns", region_name="us-east-1")
mock_topic = conn.create_topic(Name="mocktopic")
topic_arn = mock_topic.get("TopicArn")
os.environ["SNS_TOPIC"] = topic_arn
# call my_function
my_module.my_method()
还打开了 GitHub 问题:https ://github.com/spulec/moto/issues/3027
解决方案
也许它将帮助您
将所有模块保留在一个类中,并在该类上放置一个装饰器 @mock_sns 以模拟 sns,还将装饰器 @mock_sns 放置在您初始化与 sns 连接的函数上。例子:
@mock_sns
class TestSnsMock(unittest.TestCase):
@classmethod
@mock_sns
def setUpClass(cls):
cls.conn = boto3.client("sns", region_name="us-east-1")
cls.conn.create_topic(Name="some-topic")
cls.response = cls.conn.list_topics()
cls.topic_arn = cls.response["Topics"][0]["TopicArn"]
def test_publish_sns(self):
message = "here is same message"
self.sns_client.publish(TopicArn=self.topic_arn, Message=message)
if __name__ == "__main__":
unittest.main()
推荐阅读
- python - 我怎样才能让这个冒泡排序更 Pythonic?
- python - ZeroDivisionError:除以零错误,py
- reactjs - 将 html em 标签转换为反应文本组件
- javascript - 反应:仅发送数组
- python - 将包含 "b'...'" 的字符串转换为 unicode
- email - LogicApp:在触发新电子邮件后触发后使用 office365 Outlook 发送电子邮件操作无法发送附加的 msg 文件。我怎样才能避免它?
- python - 是否有用于发送数百个机器人的好的 Python 模块?
- python - Sklearn 模型的大小不断增加
- python - Pandas 数据框中每 X 行满足条件的所有行的平均数据
- android - 如何在 Git Bash 中打开文件 Android Studio?