Skip to content
Snippets Groups Projects
Commit c1ffe144 authored by Michael Foord's avatar Michael Foord
Browse files

Docs update

parent 6d1ebb3b
No related branches found
No related tags found
No related merge requests found
......@@ -273,8 +273,6 @@ function; *every* patching will appear in the tuple after "as":
Where to patch
==============
`patch` works by (temporarily) changing the object that a *name* points to with
another one. There can be many names pointing to any individual object, so
for patching to work you must ensure that you patch the name used by the system
......@@ -284,6 +282,29 @@ The basic principle is that you patch where an object is *used*, which is not
necessarily the same place as where it is defined. A couple of examples will
help to clarify this.
Imagine we have a project that we want to test with the following structure::
a.py
-> Defines SomeClass
b.py
-> from a import SomeClass
-> some_function instantiates SomeClass
Now we want to test `some_function` but we want to mock out `SomeClass` using
`patch`. The problem is that when we import module b, which we will have to
do then it imports `SomeClass` from module a. If we use `patch` to mock out
`a.SomeClass` then it will have no effect on our test; module b already has a
reference to the *real* `SomeClass` and it looks like our patching had no
effect.
The key is to patch out `SomeClass` where it is used (or where it is looked up
). In this case `some_function` will actually look up `SomeClass` in module b,
where we have imported it. The patching should look like:
``@patch('b.SomeClass')``
However, consider the alternative scenario where
Patching Descriptors and Proxy Objects
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment