Hi I am trying to test to see if a method in object A calls another object, object B's, methods. I already have separate tests which tests B's functionality so my goal is just to see if B's method has been called. I am trying to use mocks to create a mock object B, following a method similar to this, but keep getting the following error: AssertionError: Expected 'process' to have been called once. Called 0 times.
Am I doing the mock wrong?
From my understanding, this answer suggested to access the field in sut and set it to a mock, but due to how my code is setup I can not access the other objects.
Example code:
# object B
from abc import ABCMeta
class B(metaclass=ABCMeta):
def process(self):
print('I am B')
# object C
from abc import ABCMeta
class C(metaclass=ABCMeta):
def process(self):
print('I am C')
# object A
from abc import ABCMeta
from b import B
from c import C
class A(metaclass=ABCMeta):
def __init__(self):
self.__known_auto_processes = {}
self.__inti_know_processes()
def process(self, arg):
try:
self.__known_auto_processes[arg].process()
except KeyError as error:
print(f'Invalid arg option {error.args}.')
def __inti_know_processes(self):
self.__known_auto_processes['B'] = B()
self.__known_auto_processes['C'] = C()
Example Test:
import unittest
from unittest.mock import patch
from a import A
class TestA(unittest.TestCase):
@patch("b.B")
def test_b_call(self, mock_b):
a = A()
a.process('B')
mock_b.process.assert_called_once()