@@ -68,7 +68,7 @@ available, and then make assertions about how they have been used:
68
68
3
69
69
>>> thing.method.assert_called_with(3 , 4 , 5 , key = ' value' )
70
70
71
- :attr: `side_effect ` allows you to perform side effects, including raising an
71
+ :attr: `~Mock. side_effect ` allows you to perform side effects, including raising an
72
72
exception when a mock is called:
73
73
74
74
>>> from unittest.mock import Mock
@@ -737,16 +737,16 @@ the *new_callable* argument to :func:`patch`.
737
737
738
738
.. attribute :: __class__
739
739
740
- Normally the :attr: `__class__ ` attribute of an object will return its type.
741
- For a mock object with a :attr: `spec `, `` __class__ ` ` returns the spec class
740
+ Normally the :attr: `! __class__ ` attribute of an object will return its type.
741
+ For a mock object with a :attr: `! spec `, :attr: ` ! __class__ ` returns the spec class
742
742
instead. This allows mock objects to pass :func: `isinstance ` tests for the
743
743
object they are replacing / masquerading as:
744
744
745
745
>>> mock = Mock(spec = 3 )
746
746
>>> isinstance (mock, int )
747
747
True
748
748
749
- :attr: `__class__ ` is assignable to, this allows a mock to pass an
749
+ :attr: `! __class__ ` is assignable to, this allows a mock to pass an
750
750
:func: `isinstance ` check without forcing you to use a spec:
751
751
752
752
>>> mock = Mock()
@@ -760,8 +760,8 @@ the *new_callable* argument to :func:`patch`.
760
760
meaning of :class: `Mock `, with the exception of *return_value * and *side_effect *
761
761
which have no meaning on a non-callable mock.
762
762
763
- Mock objects that use a class or an instance as a :attr: `spec ` or
764
- :attr: `spec_set ` are able to pass :func: `isinstance ` tests:
763
+ Mock objects that use a class or an instance as a :attr: `! spec ` or
764
+ :attr: `! spec_set ` are able to pass :func: `isinstance ` tests:
765
765
766
766
>>> mock = Mock(spec = SomeClass)
767
767
>>> isinstance (mock, SomeClass)
@@ -1175,7 +1175,7 @@ Calls made to the object will be recorded in the attributes
1175
1175
like :attr: `~Mock.call_args ` and :attr: `~Mock.call_args_list `.
1176
1176
1177
1177
If :attr: `~Mock.side_effect ` is set then it will be called after the call has
1178
- been recorded, so if :attr: `side_effect ` raises an exception the call is still
1178
+ been recorded, so if :attr: `! side_effect ` raises an exception the call is still
1179
1179
recorded.
1180
1180
1181
1181
The simplest way to make a mock raise an exception when called is to make
@@ -1196,8 +1196,8 @@ The simplest way to make a mock raise an exception when called is to make
1196
1196
>>> m.mock_calls
1197
1197
[call(1, 2, 3), call('two', 'three', 'four')]
1198
1198
1199
- If :attr: `side_effect ` is a function then whatever that function returns is what
1200
- calls to the mock return. The :attr: `side_effect ` function is called with the
1199
+ If :attr: `~Mock. side_effect ` is a function then whatever that function returns is what
1200
+ calls to the mock return. The :attr: `! side_effect ` function is called with the
1201
1201
same arguments as the mock. This allows you to vary the return value of the
1202
1202
call dynamically, based on the input:
1203
1203
@@ -1214,7 +1214,7 @@ call dynamically, based on the input:
1214
1214
1215
1215
If you want the mock to still return the default return value (a new mock), or
1216
1216
any set return value, then there are two ways of doing this. Either return
1217
- :attr: `mock .return_value ` from inside :attr: `side_effect `, or return :data: `DEFAULT `:
1217
+ :attr: `~Mock .return_value ` from inside :attr: `~Mock. side_effect `, or return :data: `DEFAULT `:
1218
1218
1219
1219
>>> m = MagicMock()
1220
1220
>>> def side_effect (* args , ** kwargs ):
@@ -1231,8 +1231,8 @@ any set return value, then there are two ways of doing this. Either return
1231
1231
>>> m()
1232
1232
3
1233
1233
1234
- To remove a :attr: `side_effect `, and return to the default behaviour, set the
1235
- :attr: `side_effect ` to ``None ``:
1234
+ To remove a :attr: `~Mock. side_effect `, and return to the default behaviour, set the
1235
+ :attr: `! side_effect ` to ``None ``:
1236
1236
1237
1237
>>> m = MagicMock(return_value = 6 )
1238
1238
>>> def side_effect (* args , ** kwargs ):
@@ -1245,7 +1245,7 @@ To remove a :attr:`side_effect`, and return to the default behaviour, set the
1245
1245
>>> m()
1246
1246
6
1247
1247
1248
- The :attr: `side_effect ` can also be any iterable object. Repeated calls to the mock
1248
+ The :attr: `~Mock. side_effect ` can also be any iterable object. Repeated calls to the mock
1249
1249
will return values from the iterable (until the iterable is exhausted and
1250
1250
a :exc: `StopIteration ` is raised):
1251
1251
@@ -1286,7 +1286,7 @@ objects of any type.
1286
1286
1287
1287
You may want a mock object to return ``False `` to a :func: `hasattr ` call, or raise an
1288
1288
:exc: `AttributeError ` when an attribute is fetched. You can do this by providing
1289
- an object as a :attr: `spec ` for a mock, but that isn't always convenient.
1289
+ an object as a :attr: `! spec ` for a mock, but that isn't always convenient.
1290
1290
1291
1291
You "block" attributes by deleting them. Once deleted, accessing an attribute
1292
1292
will raise an :exc: `AttributeError `.
@@ -1455,7 +1455,7 @@ patch
1455
1455
If you are patching builtins in a module then you don't
1456
1456
need to pass ``create=True ``, it will be added by default.
1457
1457
1458
- Patch can be used as a :class: `TestCase ` class decorator. It works by
1458
+ Patch can be used as a :class: `~unittest. TestCase ` class decorator. It works by
1459
1459
decorating each test method in the class. This reduces the boilerplate
1460
1460
code when your test methods share a common patchings set. :func: `patch ` finds
1461
1461
tests by looking for method names that start with ``patch.TEST_PREFIX ``.
@@ -1493,7 +1493,7 @@ If the class is instantiated multiple times you could use
1493
1493
can set the *return_value * to be anything you want.
1494
1494
1495
1495
To configure return values on methods of *instances * on the patched class
1496
- you must do this on the :attr: `return_value `. For example::
1496
+ you must do this on the :attr: `~Mock. return_value `. For example::
1497
1497
1498
1498
>>> class Class:
1499
1499
... def method(self):
@@ -1814,13 +1814,13 @@ context manager is a dictionary where created mocks are keyed by name::
1814
1814
patch methods: start and stop
1815
1815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1816
1816
1817
- All the patchers have :meth: `start ` and :meth: `stop ` methods. These make it simpler to do
1817
+ All the patchers have :meth: `! start ` and :meth: `! stop ` methods. These make it simpler to do
1818
1818
patching in ``setUp `` methods or where you want to do multiple patches without
1819
1819
nesting decorators or with statements.
1820
1820
1821
1821
To use them call :func: `patch `, :func: `patch.object ` or :func: `patch.dict ` as
1822
1822
normal and keep a reference to the returned ``patcher `` object. You can then
1823
- call :meth: `start ` to put the patch in place and :meth: `stop ` to undo it.
1823
+ call :meth: `! start ` to put the patch in place and :meth: `! stop ` to undo it.
1824
1824
1825
1825
If you are using :func: `patch ` to create a mock for you then it will be returned by
1826
1826
the call to ``patcher.start ``. ::
@@ -1837,7 +1837,7 @@ the call to ``patcher.start``. ::
1837
1837
1838
1838
1839
1839
A typical use case for this might be for doing multiple patches in the ``setUp ``
1840
- method of a :class: `TestCase `::
1840
+ method of a :class: `~unittest. TestCase `::
1841
1841
1842
1842
>>> class MyTest(unittest.TestCase):
1843
1843
... def setUp(self):
@@ -2510,7 +2510,7 @@ behaviour you can switch it off by setting the module level switch
2510
2510
2511
2511
Alternatively you can just use ``vars(my_mock) `` (instance members) and
2512
2512
``dir(type(my_mock)) `` (type members) to bypass the filtering irrespective of
2513
- :const: `mock. FILTER_DIR `.
2513
+ :const: `FILTER_DIR `.
2514
2514
2515
2515
2516
2516
mock_open
@@ -2525,7 +2525,7 @@ mock_open
2525
2525
default) then a :class: `MagicMock ` will be created for you, with the API limited
2526
2526
to methods or attributes available on standard file handles.
2527
2527
2528
- *read_data * is a string for the :meth: `~io.IOBase .read `,
2528
+ *read_data * is a string for the :meth: `~io.RawIOBase .read `,
2529
2529
:meth: `~io.IOBase.readline `, and :meth: `~io.IOBase.readlines ` methods
2530
2530
of the file handle to return. Calls to those methods will take data from
2531
2531
*read_data * until it is depleted. The mock of these methods is pretty
@@ -2537,7 +2537,7 @@ mock_open
2537
2537
2538
2538
.. versionchanged :: 3.4
2539
2539
Added :meth: `~io.IOBase.readline ` and :meth: `~io.IOBase.readlines ` support.
2540
- The mock of :meth: `~io.IOBase .read ` changed to consume *read_data * rather
2540
+ The mock of :meth: `~io.RawIOBase .read ` changed to consume *read_data * rather
2541
2541
than returning it on each call.
2542
2542
2543
2543
.. versionchanged :: 3.5
@@ -2589,7 +2589,7 @@ And for reading files::
2589
2589
Autospeccing
2590
2590
~~~~~~~~~~~~
2591
2591
2592
- Autospeccing is based on the existing :attr: `spec ` feature of mock. It limits the
2592
+ Autospeccing is based on the existing :attr: `! spec ` feature of mock. It limits the
2593
2593
api of mocks to the api of an original object (the spec), but it is recursive
2594
2594
(implemented lazily) so that attributes of mocks only have the same api as
2595
2595
the attributes of the spec. In addition mocked functions / methods have the
@@ -2614,8 +2614,8 @@ unit tests. Testing everything in isolation is all fine and dandy, but if you
2614
2614
don't test how your units are "wired together" there is still lots of room
2615
2615
for bugs that tests might have caught.
2616
2616
2617
- :mod: `mock ` already provides a feature to help with this, called speccing. If you
2618
- use a class or instance as the :attr: `spec ` for a mock then you can only access
2617
+ :mod: `unittest. mock ` already provides a feature to help with this, called speccing. If you
2618
+ use a class or instance as the :attr: `! spec ` for a mock then you can only access
2619
2619
attributes on the mock that exist on the real class:
2620
2620
2621
2621
>>> from urllib import request
@@ -2653,7 +2653,7 @@ Here's an example of it in use::
2653
2653
>>> mock_request.Request
2654
2654
<MagicMock name='request.Request' spec='Request' id='...'>
2655
2655
2656
- You can see that :class: `request.Request ` has a spec. :class: `request.Request ` takes two
2656
+ You can see that :class: `! request.Request ` has a spec. :class: `! request.Request ` takes two
2657
2657
arguments in the constructor (one of which is *self *). Here's what happens if
2658
2658
we try to call it incorrectly::
2659
2659
@@ -2669,8 +2669,8 @@ specced mocks)::
2669
2669
>>> req
2670
2670
<NonCallableMagicMock name='request.Request()' spec='Request' id='...'>
2671
2671
2672
- :class: `Request ` objects are not callable, so the return value of instantiating our
2673
- mocked out :class: `request.Request ` is a non-callable mock. With the spec in place
2672
+ :class: `! Request ` objects are not callable, so the return value of instantiating our
2673
+ mocked out :class: `! request.Request ` is a non-callable mock. With the spec in place
2674
2674
any typos in our asserts will raise the correct error::
2675
2675
2676
2676
>>> req.add_header('spam', 'eggs')
@@ -2822,8 +2822,8 @@ Sealing mocks
2822
2822
.. versionadded :: 3.7
2823
2823
2824
2824
2825
- Order of precedence of :attr: `side_effect `, :attr: `return_value ` and *wraps *
2826
- ----------------------------------------------------------------------------
2825
+ Order of precedence of :attr: `! side_effect `, :attr: `! return_value ` and *wraps *
2826
+ ------------------------------------------------------------------------------
2827
2827
2828
2828
The order of their precedence is:
2829
2829
0 commit comments