How to Mock a missing attribute

You can delete the attribute, which will cause hasattr to return False.

From the Documentation:

>>> mock = MagicMock()
>>> hasattr(mock, 'm')
True
>>> del mock.m
>>> hasattr(mock, 'm')
False
>>> del mock.f
>>> mock.f
Traceback (most recent call last):
    ...
AttributeError: f

For your specific example, since mock_class.a is another Mock, you can do del mock_class.a.c.


Actually mock_class.a will create another MagicMock, which don't have a spec. The only way I can think of is to assign the attribute a of the mock_class with another MagicMock with spec, like this:

mock_class = MagicMock(spec=[u'a'])
mock_class.a = MagicMock(spec=[u'a'])
hasattr(mock_class.a, u'c')  # returns False

Also if you have some real objects you want to mock, there is a possibility to do some recursive autospecing.

Tags:

Python

Mocking