summaryrefslogtreecommitdiff
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorMario Corchero <mariocj89@gmail.com>2018-12-08 11:25:02 +0000
committerChris Withers <chris@withers.org>2018-12-08 11:25:02 +0000
commitf05df0a4b679d0acfd0b1fe6187ba2d553b37afa (patch)
tree5a048fb3c7aef3ffebb5a955ae2259823d0b1d8f /Lib/unittest/mock.py
parent3cf74384b53b998fa846dc2590cedf9ad2a0d5fd (diff)
downloadcpython-git-f05df0a4b679d0acfd0b1fe6187ba2d553b37afa.tar.gz
bpo-35330: Don't call the wrapped object if `side_effect` is set (GH10973)
* tests: Further validate `wraps` functionality in `unittest.mock.Mock` Add more tests to validate how `wraps` interacts with other features of mocks. * Don't call the wrapped object if `side_effect` is set When a object is wrapped using `Mock(wraps=...)`, if an user sets a `side_effect` in one of their methods, return the value of `side_effect` and don't call the original object. * Refactor what to be called on `mock_call` When a `Mock` is called, it should return looking up in the following order: `side_effect`, `return_value`, `wraps`. If any of the first two return `mock.DEFAULT`, lookup in the next option. It makes no sense to check for `wraps` returning default, as it is supposed to be the original implementation and there is nothing to fallback to.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py21
1 files changed, 10 insertions, 11 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index b75a1fa5cf..61ed80b976 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1027,28 +1027,27 @@ class CallableMixin(Base):
break
seen.add(_new_parent_id)
- ret_val = DEFAULT
effect = self.side_effect
if effect is not None:
if _is_exception(effect):
raise effect
-
- if not _callable(effect):
+ elif not _callable(effect):
result = next(effect)
if _is_exception(result):
raise result
- if result is DEFAULT:
- result = self.return_value
+ else:
+ result = effect(*args, **kwargs)
+
+ if result is not DEFAULT:
return result
- ret_val = effect(*args, **kwargs)
+ if self._mock_return_value is not DEFAULT:
+ return self.return_value
- if (self._mock_wraps is not None and
- self._mock_return_value is DEFAULT):
+ if self._mock_wraps is not None:
return self._mock_wraps(*args, **kwargs)
- if ret_val is DEFAULT:
- ret_val = self.return_value
- return ret_val
+
+ return self.return_value