I want to mock a class method's output, which is called by a function defined in a different module. For example:
class_module.py
class my_class:
def __init__(self, user):
self.user = user
def my_method(self):
return [1,2,3]
def other_methods(self):
other_func(self.user)
return "something I do not really want to mock"
function_module.py
from class_module import my_class
def my_fun():
user = "me"
foo = my_class(user)
foo.other_methods()
return foo.my_method()
test.py
@patch("function_module.my_class")
def test_my_fun(class_mock):
class_mock.return_value.my_method.return_value = []
bar = my_fun()
assert bar == []
However, I am getting an AssertionError
saying that [1,2,3] != []
. So I guess the mock is never happening on the method I want. Can somebody explain how to do it? And why is this happening?
EDIT
Actually, the implementation shown does not work because the test is starting an entirely separate process. Therefore, no function could be mocked. Sorry for my misconception