@@ -418,14 +418,14 @@ decorator indvidually to every method whose name starts with "test".
418418.. _further-examples :
419419
420420Further Examples
421- ================
421+ ----------------
422422
423423
424424Here are some more examples for some slightly more advanced scenarios.
425425
426426
427427Mocking chained calls
428- ---------------------
428+ ~~~~~~~~~~~~~~~~~~~~~
429429
430430Mocking chained calls is actually straightforward with mock once you
431431understand the :attr: `~Mock.return_value ` attribute. When a mock is called for
@@ -496,7 +496,7 @@ this list of calls for us:
496496
497497
498498Partial mocking
499- ---------------
499+ ~~~~~~~~~~~~~~~
500500
501501In some tests I wanted to mock out a call to `datetime.date.today()
502502<http://docs.python.org/library/datetime.html#datetime.date.today> `_ to return
@@ -540,7 +540,7 @@ is discussed in `this blog entry
540540
541541
542542Mocking a Generator Method
543- --------------------------
543+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
544544
545545A Python generator is a function or method that uses the `yield statement
546546<http://docs.python.org/reference/simple_stmts.html#the-yield-statement> `_ to
@@ -582,7 +582,7 @@ To configure the values returned from the iteration (implicit in the call to
582582
583583
584584 Applying the same patch to every test method
585- --------------------------------------------
585+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
586586
587587If you want several patches in place for multiple test methods the obvious way
588588is to apply the patch decorators to every method. This can feel like unnecessary
@@ -642,7 +642,7 @@ exception is raised in the setUp then tearDown is not called.
642642
643643
644644Mocking Unbound Methods
645- -----------------------
645+ ~~~~~~~~~~~~~~~~~~~~~~~
646646
647647Whilst writing tests today I needed to patch an *unbound method * (patching the
648648method on the class rather than on the instance). I needed self to be passed
@@ -681,7 +681,7 @@ with a Mock instance instead, and isn't called with `self`.
681681
682682
683683Checking multiple calls with mock
684- ---------------------------------
684+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
685685
686686mock has a nice API for making assertions about how your mock objects are used.
687687
@@ -723,7 +723,7 @@ looks remarkably similar to the repr of the `call_args_list`:
723723
724724
725725Coping with mutable arguments
726- -----------------------------
726+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727727
728728Another situation is rare, but can bite you, is when your mock is called with
729729mutable arguments. `call_args ` and `call_args_list ` store *references * to the
@@ -839,7 +839,7 @@ children of a `CopyingMock` will also have the type `CopyingMock`.
839839
840840
841841Nesting Patches
842- ---------------
842+ ~~~~~~~~~~~~~~~
843843
844844Using patch as a context manager is nice, but if you do multiple patches you
845845can end up with nested with statements indenting further and further to the
@@ -887,7 +887,7 @@ for us:
887887
888888
889889Mocking a dictionary with MagicMock
890- -----------------------------------
890+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
891891
892892You may want to mock a dictionary, or other container object, recording all
893893access to it whilst having it still behave like a dictionary.
@@ -962,7 +962,7 @@ mock methods and attributes:
962962
963963
964964Mock subclasses and their attributes
965- ------------------------------------
965+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
966966
967967There are various reasons why you might want to subclass `Mock `. One reason
968968might be to add helper methods. Here's a silly example:
@@ -1025,7 +1025,7 @@ onto the mock constructor:
10251025
10261026
10271027 Mocking imports with patch.dict
1028- -------------------------------
1028+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10291029
10301030One situation where mocking can be hard is where you have a local import inside
10311031a function. These are harder to mock because they aren't using an object from
@@ -1088,7 +1088,7 @@ With slightly more work you can also mock package imports:
10881088
10891089
10901090Tracking order of calls and less verbose call assertions
1091- --------------------------------------------------------
1091+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10921092
10931093The :class: `Mock ` class allows you to track the *order * of method calls on
10941094your mock objects through the :attr: `~Mock.method_calls ` attribute. This
@@ -1168,7 +1168,7 @@ order. In this case you can pass `any_order=True` to `assert_has_calls`:
11681168
11691169
11701170More complex argument matching
1171- ------------------------------
1171+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11721172
11731173Using the same basic concept as :data: `ANY ` we can implement matchers to do more
11741174complex assertions on objects used as arguments to mocks.
0 commit comments