I have a Django app called "publisher", it connects to various signals in my django project, and when it receives them it sends a message to a rabbitmq queue. What I want to do is to be able to test that my setup code is connecting to the correct signals.
My app structure looks like:
def receiver_function(*args, **kwargs):
#Does rabbitmq stuff
with patch('publisher.signals.receiver_function') as receiver_mock:
I ran into the same mocking problem you describe. My solution is to reach into Django's signal registry and assert that my function was registered with the correct signal.
Here's my test:
def test_signal_registry(self): from foo.models import bar_func # The function I want to register. from django.db.models import signals registered_functions = [r() for r in signals.pre_delete.receivers] self.assertIn(bar_func, registered_functions)
A little explanation about that list comprehension:
"pre_delete" is the instance of django.dispatch.dispatcher.Signal that I cared about in this case. You would be using your own "my_interesting_signal" in your example. Signals have an internal property called "receivers" that's a list of two-tuples, where the second element is a weakref to the function you register (hence r). Calling a weakref returns the referent.
I had to play around with weakrefs to figure that much out:
import weakref def foo(): pass w = weakref.ref(foo) w() == foo
Hope this helps.