hRobert12 / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Patching doesnt work if methods are imported from module #171

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.patch doesnt work if methods are imported from module

from mock import patch

from util.clock import now
from util import clock

with patch('util.clock.now',return_value=666) as _:
        print now(), clock.now()

What is the expected output? What do you see instead?
now() call doesnt get patched but clock.now() gets patched

Expected: 666 666
Actual: 2012-09-03 12:59:31.994025 666
What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by R.Raghun...@gmail.com on 3 Sep 2012 at 7:35

GoogleCodeExporter commented 8 years ago
typo in title:  Patching doesnt work if methods are imported from module

Original comment by R.Raghun...@gmail.com on 3 Sep 2012 at 7:36

GoogleCodeExporter commented 8 years ago
Patching only replaces individual references. To patch functions / classes that 
you import you need to patch them where they are looked up not where they're 
defined.

See the documentation section "where to patch":

http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

Original comment by fuzzyman on 3 Sep 2012 at 8:36