Permalink
Browse files

Add context manager to enable mocking of signal handlers

  • Loading branch information...
1 parent c5bff3c commit bdb5497e241c193a19869fd8e014b35075e580e1 Moose committed with codeinthehole Mar 5, 2013
Showing with 28 additions and 1 deletion.
  1. +26 −0 oscar_testsupport/contextmanagers.py
  2. +2 −1 setup.py
@@ -0,0 +1,26 @@
+from contextlib import contextmanager
+from mock import Mock
+
+
+@contextmanager
+def mock_signal_receiver(signal, wraps=None, **kwargs):
+ """
+ Temporarily attaches a receiver to the provided ``signal`` within the scope
+ of the context manager.
+
+ Example use::
+
+ with mock_signal_receiver(signal) as receiver:
+ # Do the thing that should trigger the signal
+ self.assertEquals(receiver.call_count, 1)
+
+ Implementation based on:
+ https://github.com/dcramer/mock-django/blob/master/mock_django/signals.py
+ """
+ if wraps is None:
+ wraps = lambda *args, **kwargs: None
+
+ receiver = Mock(wraps=wraps)
+ signal.connect(receiver, **kwargs)
+ yield receiver
+ signal.disconnect(receiver)
View
@@ -19,5 +19,6 @@
'django-oscar>=0.4,<0.6',
'WebTest>=1.3,<2.1',
'django-webtest>=1.5.7,<1.6',
- 'purl>=0.4',
+ 'purl>=0.4,<0.5',
+ 'mock>=1.0.1,<1.1'
])

0 comments on commit bdb5497

Please sign in to comment.