Skip to content

Commit b9d4cef

Browse files
miss-islingtonoklenaCAM-Gerlachpicnixz
authored
[3.13] gh-62480: De-personalize "Mocking Unbound Methods" section in unittest.mock examples (GH-141322) (#141325)
gh-62480: De-personalize "Mocking Unbound Methods" section in `unittest.mock` examples (GH-141322) * Rewrite Mocking Unbound Methods paragraph to second person (cherry picked from commit ec85d3c) Co-authored-by: Elena O <31424287+oklena@users.noreply.github.com> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 5ad2842 commit b9d4cef

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Doc/library/unittest.mock-examples.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -743,25 +743,24 @@ exception is raised in the setUp then tearDown is not called.
743743
Mocking Unbound Methods
744744
~~~~~~~~~~~~~~~~~~~~~~~
745745

746-
Whilst writing tests today I needed to patch an *unbound method* (patching the
747-
method on the class rather than on the instance). I needed self to be passed
748-
in as the first argument because I want to make asserts about which objects
749-
were calling this particular method. The issue is that you can't patch with a
750-
mock for this, because if you replace an unbound method with a mock it doesn't
751-
become a bound method when fetched from the instance, and so it doesn't get
752-
self passed in. The workaround is to patch the unbound method with a real
753-
function instead. The :func:`patch` decorator makes it so simple to
754-
patch out methods with a mock that having to create a real function becomes a
755-
nuisance.
746+
Sometimes a test needs to patch an *unbound method*, which means patching the
747+
method on the class rather than on the instance. In order to make assertions
748+
about which objects were calling this particular method, you need to pass
749+
``self`` as the first argument. The issue is that you can't patch with a mock for
750+
this, because if you replace an unbound method with a mock it doesn't become
751+
a bound method when fetched from the instance, and so it doesn't get ``self``
752+
passed in. The workaround is to patch the unbound method with a real function
753+
instead. The :func:`patch` decorator makes it so simple to patch out methods
754+
with a mock that having to create a real function becomes a nuisance.
756755

757756
If you pass ``autospec=True`` to patch then it does the patching with a
758757
*real* function object. This function object has the same signature as the one
759758
it is replacing, but delegates to a mock under the hood. You still get your
760759
mock auto-created in exactly the same way as before. What it means though, is
761760
that if you use it to patch out an unbound method on a class the mocked
762761
function will be turned into a bound method if it is fetched from an instance.
763-
It will have ``self`` passed in as the first argument, which is exactly what I
764-
wanted:
762+
It will have ``self`` passed in as the first argument, which is exactly what
763+
was needed:
765764

766765
>>> class Foo:
767766
... def foo(self):

0 commit comments

Comments
 (0)