@@ -743,25 +743,24 @@ exception is raised in the setUp then tearDown is not called.
743743Mocking 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
757756If 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
759758it is replacing, but delegates to a mock under the hood. You still get your
760759mock auto-created in exactly the same way as before. What it means though, is
761760that if you use it to patch out an unbound method on a class the mocked
762761function 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