'Patch: AssertionError: Expected '...' to have been called once. Called 0 times

I have the following class to be tested.

mod1/file1.py

@dataclass
class Base(metaclass=ABCMeta):

    account: Account
    ec2_id: str = ""

    def start(self):
        self.account.session.resource("ec2").instances.filter(
            InstanceIds=[self.ec2_id]
        ).start()

And I created the following test,

from unittest.mock import Mock, patch

    @patch('mod1.file1.Base')
    def test_start(sut):
        sut.account = Mock()
        sut.ec2_id = 'test'
        sut.start()  # sut.return_value.start() gets the same error
        sut.account.session.resource('ec2').instances.filter.assert_called_once()

However, the test failed with error

AssertionError: Expected 'filter' to have been called once. Called 0 times.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source