'Mocking two functions breaks the loop?

I am trying to test out my function by mocking two other functions inside it: My code is as follows:

import random

def generate_numbers():
    res = []
    for i in range(3):
        res.append(random.randint(1,10))
    return res

def my_func():
    numbers = generate_numbers()
    import pdb
    pdb.set_trace()
    for number in numbers:
        print(number)
        pdb.set_trace()

        if number == 3:
            empty_function(number)
        if number == 9:
            empty_function(number)


    return None

def empty_function(number):
    print('test')

And my test is like this:

from unittest.mock import patch
from func import my_func


@patch('func.generate_numbers')
@patch('func.empty_function')
def test_my_func(mock_numbers, mock_empty_function):
    mock_numbers.return_value = [2, 3, 2]
    my_func()
    mock_empty_function.assert_called_once()
    

The issue I have here is that when I was just mocking generate numbers function, everything was working as expected. But then I decided I want to assert wheter the function empty_function was called and that's where the problem is. Since I mocked the function the loop doesn't seem to be entered and no matter what numbers I mock into generate_numbers, the assert always passes, which is obviously wrong.

Anyone here who could have a look into this and help me out?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source