raise AttributeError("Mock object has no attribute %r" % name)
elif _is_magic(name):
raise AttributeError(name)
- if not self._mock_unsafe:
+ if not self._mock_unsafe and (not self._mock_methods or name not in self._mock_methods):
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
raise AttributeError(
f"{name!r} is not a valid assertion. Use a spec "
m.aseert_foo_call()
m.assrt_foo_call()
+ # gh-100739
+ def test_mock_safe_with_spec(self):
+ class Foo(object):
+ def assert_bar(self):
+ pass
+
+ def assertSome(self):
+ pass
+
+ m = Mock(spec=Foo)
+ m.assert_bar()
+ m.assertSome()
+
+ m.assert_bar.assert_called_once()
+ m.assertSome.assert_called_once()
+
#Issue21262
def test_assert_not_called(self):
m = Mock()
--- /dev/null
+Fix ``unittest.mock.Mock`` not respecting the spec for attribute names prefixed with ``assert``.