0

I'm using Python 3.8. I have a class with a member field ...

class AbcServiceBus:

    def __init__(self, ...):
        ...
        self._service_bus = AzureServiceBus()

    def send_insert_notification(self, record_id):
        message_json = {'ids': [record_id]} 
        self._service_bus.send_topic_message(
            namespace_name=self._namespace,
            topic_name=self._topic_name, 
            message_json=message_json
        )  
        return True

I would like to mock the "send_topic_message" method of the member field. I tried the below

from unittest import mock
...

    sb = AbcServiceBus(device)
    with mock.patch('common.azure_service_bus.AzureServiceBus.send_topic_message') as send_topic_message_mock:
        sb.send_insert_notification(record_id)
        send_topic_message_mock.assert_called_with(
            sb._namespace, 
            sb._topic_name, 
            {'ids': [record_id]}
        )

but this continues to call the real method of the class, as opposed to the mock I have set up. What's the proper way to mock the method of the member field?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • Looks like you don't patch `AzureServiceBus` [where it is used](https://docs.python.org/3/library/unittest.mock.html#id6) - you need to patch `using_module.AzureServiceBus.send_topic_message` instead. – MrBean Bremen Oct 20 '20 at 16:06

1 Answers1

0

I will mock the AzureServiceBus class. Create the instance of AbcServiceBus after mocking the AzureServiceBus class so that we can use the mocked version of AzureServiceBus

azure_service_bus.py:

from azure.servicebus.control_client import ServiceBusService as AzureServiceBus


class AbcServiceBus:

    def __init__(self):
        self._namespace = '_namespace'
        self._topic_name = '_topic_name'
        self._service_bus = AzureServiceBus()

    def send_insert_notification(self, record_id):
        message_json = {'ids': [record_id]}
        self._service_bus.send_topic_message(
            namespace_name=self._namespace,
            topic_name=self._topic_name,
            message_json=message_json
        )
        return True

test_azure_service_bus.py:

import unittest
from unittest import mock
from azure_service_bus import AbcServiceBus


class TestAbcServiceBus(unittest.TestCase):
    def test_send_insert_notification(self):
        record_id = '1'

        with mock.patch('azure_service_bus.AzureServiceBus') as mock_AzureServiceBus:
            mock_AzureServiceBus_instance = mock_AzureServiceBus.return_value
            sb = AbcServiceBus()
            actual = sb.send_insert_notification(record_id)
            self.assertTrue(actual)
            mock_AzureServiceBus.assert_called_once()
            mock_AzureServiceBus_instance.send_topic_message.assert_called_with(
                namespace_name='_namespace',
                topic_name='_topic_name',
                message_json={'ids': ['1']}
            )


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

unit test results:

.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Name                                                   Stmts   Miss  Cover   Missing
------------------------------------------------------------------------------------
src/stackoverflow/64447772/azure_service_bus.py           10      0   100%
src/stackoverflow/64447772/test_azure_service_bus.py      15      0   100%
------------------------------------------------------------------------------------
TOTAL                                                     25      0   100%
Lin Du
  • 88,126
  • 95
  • 281
  • 483