Mock return_value

Ben Finney ben+python at benfinney.id.au
Mon Mar 9 15:10:05 EDT 2015


Daniel <daniel.watrous at gmail.com> writes:

> I have a dao.py module with a dao class declared and I want to use
> mock to set a return value for a dao function,
> dao.execute_ldap_search().

You have found a change which worked, but you might not have understood
why yet.

The documentation for the ‘unittest.mock’ library covers the wrinkles of
where to patch an object:

    26.4.3.8. 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 under test.

    The basic principle is that you patch where an object is looked up,
    which is not necessarily the same place as where it is defined. A
    couple of examples will help to clarify this.

    […]

    <URL:https://docs.python.org/3/library/unittest.mock.html#where-to-patch>

Good hunting!

-- 
 \     “There will be a Moscow Exhibition of the Arts by 15,000 Soviet |
  `\     Republic painters and sculptors. These were executed over the |
_o__)               past two years.” —newspaper article, Soviet Weekly |
Ben Finney




More information about the Python-list mailing list