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 743B

1234567891011121314151617181920212223242526
  1. from contextlib import contextmanager
  2. from 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. wraps = lambda *args, **kwargs: None
  17. receiver = Mock(wraps=wraps)
  18. signal.connect(receiver, **kwargs)
  19. yield receiver
  20. signal.disconnect(receiver)