Mocking a signals function in a unit test

Hello,

I’m using Django version 4.2.6.

I have a file, signals.py, that includes a pre-save handler for my entity:

def do_stuff(sender, instance, **kwargs):
    ... stuff...

pre_save.connect(do_stuff, sender=MyEntity)

… so that when an instance of MyEntity is either created or updated, the do_stuff() function is executed.

My question: how do I mock do_stuff() in unit tests?

I have a number of tests that create and update MyEntity instances, so do_stuff() is being called during the execution of the tests. My implementation of do_stuff() is making a call to an external source amongst other functionality, so I want to mock it when the tests run.

I have tried declaring the following as part of my unit test:

@mock.patch("application.package.signals.do_stuff", autospec=True,)

… but when I execute the test, I can see that do_stuff() is still being executed.

Am I doing anything obviously wrong? Is there any way I can get the mocking to work?

Thank you for any assistance.