]> granicus.if.org Git - python/commitdiff
unittest.mock: a mock created by patch with a spec as the list argument will be calla...
authorMichael Foord <michael@voidspace.org.uk>
Sun, 25 Mar 2012 18:53:18 +0000 (19:53 +0100)
committerMichael Foord <michael@voidspace.org.uk>
Sun, 25 Mar 2012 18:53:18 +0000 (19:53 +0100)
Lib/unittest/mock.py
Lib/unittest/test/testmock/testpatch.py

index d73bd5385f5fb9cc954d4c6961188d26ce357bb4..4809dba59423695abca48956e0abdb1ce344d8cb 100644 (file)
@@ -1166,7 +1166,14 @@ class _patch(object):
             if new_callable is not None:
                 Klass = new_callable
             elif spec is not None or spec_set is not None:
-                if not _callable(spec or spec_set):
+                this_spec = spec
+                if spec_set is not None:
+                    this_spec = spec_set
+                if _is_list(this_spec):
+                    not_callable = '__call__' not in this_spec
+                else:
+                    not_callable = not callable(this_spec)
+                if not_callable:
                     Klass = NonCallableMagicMock
 
             if spec is not None:
index 204a30a8a03e7185e2986fdfe0c3097fe1cbd2ab..62568554da24b207f634bcf877a6d9a25d51ac61 100644 (file)
@@ -1742,6 +1742,26 @@ class PatchTest(unittest.TestCase):
                 p.stop()
 
 
+    def test_callable_spec_as_list(self):
+        spec = ('__call__',)
+        p = patch(MODNAME, spec=spec)
+        m = p.start()
+        try:
+            self.assertTrue(callable(m))
+        finally:
+            p.stop()
+
+
+    def test_not_callable_spec_as_list(self):
+        spec = ('foo', 'bar')
+        p = patch(MODNAME, spec=spec)
+        m = p.start()
+        try:
+            self.assertFalse(callable(m))
+        finally:
+            p.stop()
+
+
 
 if __name__ == '__main__':
     unittest.main()