diff options
author | Michael Foord <michael@voidspace.org.uk> | 2012-03-25 02:00:31 +0100 |
---|---|---|
committer | Michael Foord <michael@voidspace.org.uk> | 2012-03-25 02:00:31 +0100 |
commit | 92949250a4dc7870607a078bd8d7ebf60dc7fb48 (patch) | |
tree | 02b66e39a04b394d071fc9310fcdbee931143f5f | |
parent | 0687038f1a8761af2393199ccb012f7341e24d43 (diff) | |
download | mock-92949250a4dc7870607a078bd8d7ebf60dc7fb48.tar.gz |
Make using create=True with spec=True in patchers an error
-rw-r--r-- | mock.py | 8 | ||||
-rw-r--r-- | tests/testpatch.py | 15 |
2 files changed, 21 insertions, 2 deletions
@@ -1229,6 +1229,8 @@ class _patch(object): spec = original if (spec or spec_set) is not None: + if original is DEFAULT: + raise TypeError("Can't use 'spec' with create=True") if isinstance(original, ClassTypes): # If we're patching out a class and there is a spec inherit = True @@ -1273,6 +1275,8 @@ class _patch(object): "autospec creates the mock for you. Can't specify " "autospec and new." ) + if original is DEFAULT: + raise TypeError("Can't use 'spec' with create=True") spec_set = bool(spec_set) if autospec is True: autospec = original @@ -1302,7 +1306,7 @@ class _patch(object): return new - def __exit__(self, *_): + def __exit__(self, *exc_info): """Undo the patch.""" if not _is_started(self): raise RuntimeError('stop called on unstarted patcher') @@ -1320,7 +1324,7 @@ class _patch(object): del self.target for patcher in reversed(self.additional_patchers): if _is_started(patcher): - patcher.__exit__(*_) + patcher.__exit__(*exc_info) start = __enter__ stop = __exit__ diff --git a/tests/testpatch.py b/tests/testpatch.py index 54d3878..d6ebec3 100644 --- a/tests/testpatch.py +++ b/tests/testpatch.py @@ -1682,6 +1682,21 @@ class PatchTest(unittest2.TestCase): 'exception traceback not propgated') + def test_create_and_specs(self): + for kwarg in ('spec', 'spec_set', 'autospec'): + p = patch('%s.doesnotexist' % __name__, create=True, + **{kwarg: True}) + self.assertRaises(TypeError, p.start) + self.assertRaises(NameError, lambda: doesnotexist) + + # check that spec with create is innocuous if the original exists + p = patch('%s.PTModule' % __name__, create=True, + **{kwarg: True}) + try: + p.start() + finally: + p.stop() + if __name__ == '__main__': unittest2.main() |