]> granicus.if.org Git - python/commitdiff
Documentation corrections for unittest.mock
authorMichael Foord <michael@voidspace.org.uk>
Mon, 18 Mar 2013 22:04:03 +0000 (15:04 -0700)
committerMichael Foord <michael@voidspace.org.uk>
Mon, 18 Mar 2013 22:04:03 +0000 (15:04 -0700)
Doc/library/unittest.mock.rst

index ec316dbdb88e82c10b4ae9d6f020fdc630cfdc59..6d1a57e21a335930ee96d008ceeff3e0063f008b 100644 (file)
@@ -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
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~