Boto Stubber SQS

时间:2018-11-21 12:47:02

标签: boto3 stub botocore

我需要模拟boto sqs接收消息,但收到错误:

  

AttributeError:“ Stubber”对象没有属性“ receive_message”

sqs_client 属性是Stubber,但无法识别 receive_message ,我也不知道为什么。

import unittest
from api.modules.sqs_consumer import SqsConsumer
from botocore.stub import Stubber
from botocore.stub import ANY


class TestSqsConsumer(unittest.TestCase):

def test_should(self):
    # given
    sqs_consumer = SqsConsumer()
    sqs_consumer_stup = Stubber(sqs_consumer.sqs_client)

    expected_params = dict(
        QueueUrl=ANY,
        MaxNumberOfMessages=ANY,
        WaitTimeSeconds=ANY,
        MessageAttributeNames=[
            'All'
        ])

    sqs_consumer_stup.add_response(
        method='receive_message',
        service_response={},
        expected_params=expected_params
    )

    sqs_consumer_stup.activate()
    sqs_consumer.sqs_client = sqs_consumer_stup

    # when
    sqs_consumer.process()

    # then
    self.assertEqual(True, True)

if __name__ == '__main__':
    unittest.main()

1 个答案:

答案 0 :(得分:0)

Boto3存根在调用激活时对Boto客户端进行就地更新。因此您不需要此行

sqs_consumer.sqs_client = sqs_consumer_stup

以上几行将sqs_client替换为不需要的Stubber对象。