summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py21
-rw-r--r--Lib/unittest/test/testmock/testmock.py122
2 files changed, 132 insertions, 11 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index b75a1fa..61ed80b 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
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 0a638b7..193ae9f 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -558,6 +558,16 @@ class MockTest(unittest.TestCase):
real.assert_called_with(1, 2, fish=3)
+ def test_wraps_prevents_automatic_creation_of_mocks(self):
+ class Real(object):
+ pass
+
+ real = Real()
+ mock = Mock(wraps=real)
+
+ self.assertRaises(AttributeError, lambda: mock.new_attr())
+
+
def test_wraps_call_with_nondefault_return_value(self):
real = Mock()
@@ -584,6 +594,118 @@ class MockTest(unittest.TestCase):
self.assertEqual(result, Real.attribute.frog())
+ def test_customize_wrapped_object_with_side_effect_iterable_with_default(self):
+ class Real(object):
+ def method(self):
+ return sentinel.ORIGINAL_VALUE
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = [sentinel.VALUE1, DEFAULT]
+
+ self.assertEqual(mock.method(), sentinel.VALUE1)
+ self.assertEqual(mock.method(), sentinel.ORIGINAL_VALUE)
+ self.assertRaises(StopIteration, mock.method)
+
+
+ def test_customize_wrapped_object_with_side_effect_iterable(self):
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = [sentinel.VALUE1, sentinel.VALUE2]
+
+ self.assertEqual(mock.method(), sentinel.VALUE1)
+ self.assertEqual(mock.method(), sentinel.VALUE2)
+ self.assertRaises(StopIteration, mock.method)
+
+
+ def test_customize_wrapped_object_with_side_effect_exception(self):
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = RuntimeError
+
+ self.assertRaises(RuntimeError, mock.method)
+
+
+ def test_customize_wrapped_object_with_side_effect_function(self):
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ def side_effect():
+ return sentinel.VALUE
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = side_effect
+
+ self.assertEqual(mock.method(), sentinel.VALUE)
+
+
+ def test_customize_wrapped_object_with_return_value(self):
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.return_value = sentinel.VALUE
+
+ self.assertEqual(mock.method(), sentinel.VALUE)
+
+
+ def test_customize_wrapped_object_with_return_value_and_side_effect(self):
+ # side_effect should always take precedence over return_value.
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = [sentinel.VALUE1, sentinel.VALUE2]
+ mock.method.return_value = sentinel.WRONG_VALUE
+
+ self.assertEqual(mock.method(), sentinel.VALUE1)
+ self.assertEqual(mock.method(), sentinel.VALUE2)
+ self.assertRaises(StopIteration, mock.method)
+
+
+ def test_customize_wrapped_object_with_return_value_and_side_effect2(self):
+ # side_effect can return DEFAULT to default to return_value
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = lambda: DEFAULT
+ mock.method.return_value = sentinel.VALUE
+
+ self.assertEqual(mock.method(), sentinel.VALUE)
+
+
+ def test_customize_wrapped_object_with_return_value_and_side_effect_default(self):
+ class Real(object):
+ def method(self):
+ raise NotImplementedError()
+
+ real = Real()
+ mock = Mock(wraps=real)
+ mock.method.side_effect = [sentinel.VALUE1, DEFAULT]
+ mock.method.return_value = sentinel.RETURN
+
+ self.assertEqual(mock.method(), sentinel.VALUE1)
+ self.assertEqual(mock.method(), sentinel.RETURN)
+ self.assertRaises(StopIteration, mock.method)
+
+
def test_exceptional_side_effect(self):
mock = Mock(side_effect=AttributeError)
self.assertRaises(AttributeError, mock)