Document the mock attribute for mockpatch

When using mockpatch.Patch/PatchObject you need access to the
mock in order to verify that it's getting called (e.g.,
assert_called_once_with), but this attribute wasn't documented so
nobody can tell how to do it.

While adding the attribute, other issues with the docs are
corrected.

Change-Id: I7df083998adcee710cf1c5b87a1b8f091e7ad522
This commit is contained in:
Brant Knudson 2015-06-09 20:15:59 -05:00
parent 6bd1f64542
commit ecad0651b1
1 changed files with 31 additions and 17 deletions

View File

@ -28,7 +28,13 @@ class _Base(fixtures.Fixture):
class PatchObject(_Base):
"""Deal with code around mock."""
"""Deal with code around mock.
.. py:attribute:: mock
The mock.
"""
def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs):
super(PatchObject, self).__init__()
@ -36,7 +42,13 @@ class PatchObject(_Base):
class Patch(_Base):
"""Deal with code around mock.patch."""
"""Deal with code around mock.
.. py:attribute:: mock
The mock.
"""
def __init__(self, obj, new=mock.DEFAULT, **kwargs):
super(Patch, self).__init__()
@ -44,24 +56,26 @@ class Patch(_Base):
class Multiple(_Base):
"""Deal with code around mock.patch.multiple."""
"""Deal with code around mock.patch.multiple.
Pass name=value to replace obj.name with value.
Pass name= :py:attr:`Multiple.DEFAULT` to replace obj.name with a MagicMock
instance.
:param obj: Object or name containing values being mocked.
:type obj: str or object
:param kwargs: names and values of attributes of obj to be mocked.
.. py:attribute:: mock
The mock.
"""
# Default value to trigger a MagicMock to be created for a named
# attribute.
DEFAULT = mock.DEFAULT
"""Triggers a MagicMock to be created for a named attribute."""
def __init__(self, obj, **kwargs):
"""Initialize the mocks
Pass name=value to replace obj.name with value.
Pass name=Multiple.DEFAULT to replace obj.name with a
MagicMock instance.
:param obj: Object or name containing values being mocked.
:type obj: str or object
:param kwargs: names and values of attributes of obj to be mocked.
"""
super(Multiple, self).__init__()
self._get_p = lambda: mock.patch.multiple(obj, **kwargs)