Let's say I have a class declared like below:
class SampleClass(object):
def __init__(self):
self.ops = {
'key 1': self.function_a,
'key 2': self.function_b
}
def function_a(self):
print('Inside a')
def function_b(self):
print('Inside b')
def main(self, key):
self.ops[key]()
Based on the key I get, I choose and execute either of the 2 functions.
I am trying to unit test main function but I am not able to patch either function_a or function_b.
I am writing unit tests in a separate file called test.py.
When I try to patch function_a with :
self.sampleclass = SampleClass()
with patch.object(SampleClass, 'function_a', return_value=None) as test_function:
self.sampleclass.main('key1')
Control goes into function_a and Inside a is printed.
How does one mock a function that is called from a function dict?