I have the following situation.
class Class1():
def __init__(self, param1):
self.param = param1
def my_func_1(self):
return "Hello " + self.param1
class Class2():
def __init__(self):
self.instance_of_class_1 = Class1('Real')
def do_it(self):
return self.instance_of_class_1.my_func_1()
class Class3():
def __init__(self):
self.instace_of_class_2 = Class2()
def do_it(self):
return self.instace_of_class_2.do_it()
I have a test that initiates a Class3
object but I want to mock the new instance of Class1
inside the constructor of Class2
. This is what i did so far:
def test_my_classes():
with patch('my.module.Class1') as class_1_mock:
class_1_mock.my_func_1.return_value = "Hello Fake"
class_3 = Class3()
assert class_3.do_it() == 'Hello Fake' #fails
I'm guessing because Class1
constructor takes params - its not a simple patch.