Home > Blockchain >  Mocking two functions breaks the loop?
Mocking two functions breaks the loop?

Time:02-28

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?

CodePudding user response:

For multiple patches, it follows stack up approach. Parameter declaration follows bottom up mapping with the decorators.

Refer: https://docs.python.org/3/library/unittest.mock.html#nesting-patch-decorators

Hence, the correct code becomes.

from unittest.mock import patch
from func import my_func

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

Notice how empty_function is the top decorator but last parameter.

  • Related