与moto的SNS嘲笑无法正常工作

问题描述 投票:0回答:1

在我的单元测试中:

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

python amazon-web-services unit-testing boto moto
1个回答
0
投票

issue is my_module.my_method()并非仅在client = boto3.client("sns")中设置区域”>

找不到它,因为它默认是与us-east-1相比的差异区域,该区域已硬编码到单元测试中

© www.soinside.com 2019 - 2024. All rights reserved.