From: Michael Foord Date: Mon, 18 Mar 2013 22:04:03 +0000 (-0700) Subject: Documentation corrections for unittest.mock X-Git-Tag: v3.3.1rc1~34 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f5752309813368f1a511d79c8b64a0888245932a;p=thirdparty%2FPython%2Fcpython.git Documentation corrections for unittest.mock --- diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index ec316dbdb88e..6d1a57e21a33 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -867,6 +867,25 @@ will raise an `AttributeError`. AttributeError: f +Mock names and the name attribute +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Since "name" is an argument to the :class:`Mock` constructor, if you want your +mock object to have a "name" attribute you can't just pass it in at creation +time. There are two alternatives. One option is to use +:meth:`~Mock.configure_mock`:: + + >>> mock = MagicMock() + >>> mock.configure_mock(name='my_name') + >>> mock.name + 'my_name' + +A simpler option is to simply set the "name" attribute after mock creation:: + + >>> mock = MagicMock() + >>> mock.name = "foo" + + Attaching Mocks as Attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~