Konstantine Rybnikov avatar Konstantine Rybnikov committed 61a4067

implement side_effects() magic. now I'm happy

Comments (0)

Files changed (2)

 
 import types
 from functools import wraps
+import unittest
+import inspect
 
 from mock import patch
 from mock import MagicMock
     def rv_fun(*args, **kw):
         return seq.pop(0)
     return rv_fun
+
+
+def side_effect_ify(method):
+    def new_f(self, *args, **kw):
+        self._current_test_method = method
+        return self.side_effects()
+        # new_kw = kw.copy()
+        # new_kw.update({'se': self.side_effects()})
+        # rv = method(self, *args, **new_kw)
+        # return rv
+    return new_f
+
+
+class BaseTestCase(unittest.TestCase):
+    # def setUp(self):
+    #     if hasattr(self, 'side_effects'):
+    #         self._side_effects = self.side_effects()
+
+    def invoke(self, se):
+        rv = self._current_test_method(self, se)
+        return rv
+
+    @classmethod
+    def setUpClass(cls):
+        methods = filter(lambda x: inspect.ismethod(x[1]),
+                         filter(lambda x: x[0].startswith('test_'),
+                                inspect.getmembers(cls)))
+        for method_name, method in methods:
+            setattr(cls, method_name, side_effect_ify(method))
+            # if not hasattr(method, 'side_effects_decorator_added'):
+            #     setattr(getattr(cls, method_name), 'side_effects_decorator_added', True)
+
+    # def run(self, result=None):
+    #     test_method = getattr(self, self._testMethodName)
+    #     super(BaseTestCase, self).run(result=result)
 from mockstar import prefixed_p
 from mockstar import M
 from mockstar import sequence_side_effect
+from mockstar import BaseTestCase
 
 
 def side_effect_one():
         self.assertEquals(m(), 3)
 
 
+def foo():
+    return 2 + 3
+
+
+class TestSideEffectsMethod(BaseTestCase):
+    @ppatch('foo')
+    def side_effects(self, se):
+        se.something = se.foo.return_value
+        return self.invoke(se)
+
+    def test_should_get_foo_mocked(self, se=None):
+        self.assertIsInstance(se.foo(), MagicMock)
+
+    def test_should_also_get_foo_mocksed(self, se=None):
+        self.assertIsInstance(se.foo(), MagicMock)
+
+    def test_should_see_something(self, se=None):
+        self.assertIs(se.something, se.foo())
+
+
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.