Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87221

Re: Mock return_value

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Mock return_value
Date 2015-03-10 06:10 +1100
References <e32b0311-3b7b-4f7b-8f3b-212adae7f776@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.224.1425928229.21433.python-list@python.org> (permalink)

Show all headers | View raw


Daniel <daniel.watrous@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 08:27 -0700
  Re: Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 08:34 -0700
  Re: Mock return_value Ben Finney <ben+python@benfinney.id.au> - 2015-03-10 06:10 +1100
  Re: Mock return_value Daniel <daniel.watrous@gmail.com> - 2015-03-09 13:10 -0700

csiph-web