diff options
author | Michael Foord <michael@voidspace.org.uk> | 2012-03-25 19:54:05 +0100 |
---|---|---|
committer | Michael Foord <michael@voidspace.org.uk> | 2012-03-25 19:54:05 +0100 |
commit | 7b4dfa8aef73fb76736e117431a3f443884e231b (patch) | |
tree | 18b7255efb1e4059b765c99daa7472c3585e5f93 | |
parent | 855baa2cc23014a100444f5b28cdf88452c23ab4 (diff) | |
download | mock-7b4dfa8aef73fb76736e117431a3f443884e231b.tar.gz |
A mock created by patch with a spec as the list argument will be callable if __call__ is in the spec
-rw-r--r-- | docs/changelog.txt | 2 | ||||
-rw-r--r-- | mock.py | 9 | ||||
-rw-r--r-- | tests/testpatch.py | 22 |
3 files changed, 31 insertions, 2 deletions
diff --git a/docs/changelog.txt b/docs/changelog.txt index 36145d3..de9ab0b 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -27,6 +27,8 @@ The standard library version! * BUGFIX: using `spec` or `autospec` arguments to patchers, along with `spec_set=True` did not work correctly * BUGFIX: using an object that evaluates to False as a spec could be ignored +* BUGFIX: a list as the `spec` argument to a patcher would always result in a + non-callable mock. Now if `__call__` is in the spec the mock is callable 2012/02/13 Version 0.8.0 @@ -1261,7 +1261,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: diff --git a/tests/testpatch.py b/tests/testpatch.py index 443ad60..85939bb 100644 --- a/tests/testpatch.py +++ b/tests/testpatch.py @@ -6,7 +6,7 @@ import os import sys from tests import support -from tests.support import unittest2, inPy3k, SomeClass, is_instance +from tests.support import unittest2, inPy3k, SomeClass, is_instance, callable from mock import ( NonCallableMock, CallableMixin, patch, sentinel, @@ -1747,5 +1747,25 @@ class PatchTest(unittest2.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__': unittest2.main() |