4

I need to mock Boto SQS receive messages, but I receive an error:

AttributeError: 'Stubber' object has no attribute 'receive_message'

The sqs_client property is a Stubber but receive_message is not recognized, and I don't know why.

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()
rmlockerd
  • 3,776
  • 2
  • 15
  • 25
JoeLoco
  • 2,116
  • 4
  • 31
  • 59

1 Answers1

2

Boto3 stubber makes in-place update to boto client when you call activate. So you don't need this line

sqs_consumer.sqs_client = sqs_consumer_stup

The above lines replaces sqs_client with Stubber object that you don't need.

roundtheworld
  • 2,651
  • 4
  • 32
  • 51
Maneesh Singh
  • 46
  • 2
  • 4