MagicMock With Spec

Thanks to @immoralist I’ve learned a new Python testing trick. I didn’t know about the “spec” argument for MagicMock. m(
Let’s see an example:

from mock import MagicMock

class SomeModel(object):
    foo = "f**"
    bar = "b**"

m = MagicMock(spec=SomeModel)

Here we create a mock object which mimics the interface of 

SomeModel

  as we would expect, returning mock values for things we access.

>>> m.foo
<MagicMock name='mock.foo' id='4506756880'>
>>> m.bar
<MagicMock name='mock.bar' id='4506754192'>

Let’s see what happens if we call something else:

>>> m.baz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../env/lib/python2.7/site-packages/mock.py", line 658, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'baz'

It will fail loudly while a mock object without a spec would have returned a mock value as it did in the previous example.

But the magic doesn’t end there. You can still set additional attributes/methods “by hand” and have them not fail even if they aren’t part of the original spec.

>>> m.baz = "bazzzzz"
>>> m.baz
'bazzzzz'

Learning new things makes me happy. 😀

Leave a Reply

Your email address will not be published. Required fields are marked *