[Django]-Django mocks not working as expected

8👍

✅

You have to use full path to utils, e.g. @patch('my_app.utils.make_reset_password') and then in the test call a function that calls make_reset_password.

@patch('my_app.utils.make_reset_password')
def test_shouldHaveCalledMakeResetToken(self, mock_make_reset_password):
    user = User.get(...)
    function_under_test(user)
    mock_make_reset_password.assert_called_once_with(user)

EDIT

The other thing that comes to my mind is you are not mocking the correct function. If make_reset_password is imported from utils in another module then you need to change the path in the @patch decorator.

For example

# my_module.py
from my_app.utils import make_reset_password

def run_make_reset_password(user):
    make_reset_password(user)


# tests.py
@patch('my_app.my_module.make_reset_password')
def test_shouldHaveCalledMakeResetToken(self, mock_make_reset_password):
    user = User.get(...)
    run_make_reset_password(user)
    mock_make_reset_password.assert_called_once_with(user)

Leave a comment