Kaydet (Commit) e58a562d authored tarafından Michael Foord's avatar Michael Foord

unittest.mock: a mock created by patch with a spec as the list argument will be…

unittest.mock: a mock created by patch with a spec as the list argument will be callable if __call__ is in the spec
üst 87b3caf8
...@@ -1166,7 +1166,14 @@ class _patch(object): ...@@ -1166,7 +1166,14 @@ class _patch(object):
if new_callable is not None: if new_callable is not None:
Klass = new_callable Klass = new_callable
elif spec is not None or spec_set is not None: 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 Klass = NonCallableMagicMock
if spec is not None: if spec is not None:
......
...@@ -1742,6 +1742,26 @@ class PatchTest(unittest.TestCase): ...@@ -1742,6 +1742,26 @@ class PatchTest(unittest.TestCase):
p.stop() 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__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment