Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ def __aiter__():
def _set_return_value(mock, method, name):
# If _mock_wraps is present then attach it so that wrapped object
# is used for return value is used when called.
if mock._mock_wraps is not None:
if mock._mock_wraps is not None and hasattr(mock._mock_wraps, name):
method._mock_wraps = getattr(mock._mock_wraps, name)
return

Expand Down
17 changes: 17 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,23 @@ def __custom_method__(self):
self.assertEqual(obj.__custom_method__(), "foo")


def test_magic_method_wraps_with_and_without_default_value(self):
class Foo:
def __bool__(self):
return False

class Bar:
pass

# use __bool__ attribute if it is present in the wrapped object
klass1 = MagicMock(wraps=Foo)
self.assertFalse(klass1())

# use the default value if the attribute is not present
klass2 = MagicMock(wraps=Bar)
self.assertTrue(klass2())


def test_exceptional_side_effect(self):
mock = Mock(side_effect=AttributeError)
self.assertRaises(AttributeError, mock)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`unittest.mock` objects return a suitable default value when a magic
method is not defined on the mocked object.