You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

contextmanagers.py 767B

123456789101112131415161718192021222324252627
  1. from contextlib import contextmanager
  2. from unittest.mock import Mock
  3. @contextmanager
  4. def mock_signal_receiver(signal, wraps=None, **kwargs):
  5. """
  6. Temporarily attaches a receiver to the provided ``signal`` within the scope
  7. of the context manager.
  8. Example use::
  9. with mock_signal_receiver(signal) as receiver:
  10. # Do the thing that should trigger the signal
  11. self.assertEqual(receiver.call_count, 1)
  12. Implementation based on:
  13. https://github.com/dcramer/mock-django/blob/master/mock_django/signals.py
  14. """
  15. if wraps is None:
  16. def wraps(*args, **kwargs):
  17. return None
  18. receiver = Mock(wraps=wraps)
  19. signal.connect(receiver, **kwargs)
  20. yield receiver
  21. signal.disconnect(receiver)