I’m trying to write a unit test for some Python code and I’m having difficulty patching a function to have a fixed return value when said function is being called not directly but as an argument.
In file1.py
:
fn_under_test(X):
if X:
getter_fn = fn1()
else:
getter_fn = fn2()
get_all_customers(getter_fn=getter_fn()
get_all_customers(getter_fn):
return getter_fn()
In the test I have something like this:
def test():
patch('module.fn1',
auto_spec=True
return_value = ['1', '2', '3'])
fn_under_test(X=True)
In this example I’d expect the mock function to be called but instead, it seems like I’m calling the real function which in my case will make requests on the network and obviously fail as it’s operating in a unit test environment.