Reputation: 788
Following is my python module my_func.py
def get_data(request):
function_1(request)
I want to test if function_1
is called with correct request
argument inside get_data()
function and the following test works fine.
class GetDataTest(TestCase):
@patch("my_func.function_1", autospec=True)
def test_get_data(self, function_1_mock):
request_mock = MagicMock()
my_func.get_metadata(request_mock)
function_1_mock.assert_called_once_with(request_mock)
If I change my python module my_func.py
to the following where I have an array of functions then I'm struggling how to mock functions individually.
functions = [
function_1,
function_2,
function_3
]
def get_data(request):
for function in functions:
function(request)
Upvotes: 0
Views: 345
Reputation: 1145
You don't really need to mock all the functions to test this code. Instead, I would suggest replacing the list content.
First - instead of module variable, you introduce a factory method:
def get_functions():
return [
function_1,
function_2,
function_3
]
def get_data(request):
for function in get_functions():
function(request)
then you can mock get_functions()
and inject there 1 or more functions (but based on the code I don't see any value in injecting more than one):
class GetDataTest(TestCase):
@patch("my_func.function_1_mock", autospec=True)
def test_get_data(self, function_1_mock):
my_func.get_functions = MagicMock(return_value=[function_1_mock])
request_mock = MagicMock()
my_func.get_metadata(request_mock)
function_1_mock.assert_called_once_with(request_mock)
Upvotes: 1