简体   繁体   中英

Python Mocking function called inside nested functions

sorry for question that may an easy fix, am new to python mocking and having a hard time figuring this out.

 @mock.patch('my_module.PypiImportedPackage.SomeClassAdvancedFunctions.get_attrib')
    def test_mocking(self, get_attrib_mock):
       get_attrib.return_value = '1'
       my_instance = SomeClass(param1)
       mytest_result = my_instance.advanced.get_attrib('doesntmattertheinputthisismocked')
       self.assertEqual(mytest_result, 1)
       # pass 
       calltoafunctionthatoimportesSomeClassfrom__my_module__andfailsif__get_attrib__doesnotreturn1(param2,param3)
      #fail

in my_module I have

import PypiImportedPackage 
from PypiImportedPackage import SomeClass

inside SomeClass.__init__ (that I imported from pypi, so I don't have much chances to change), there is a line self.advanced = SomeClassAdvancedFunctions so I can't really patch SomeClass.advanced.get_attrib as it only exists at instance level,

which is why I had to patch PypiImportedPackage.SomeClassAdvancedFunctions and in fact this pass in my code's AssetEqual operation.

Any idea what I am doing wrong?

Thanks

Your get_attrib.return_value = '1' should be get_attrib_mock.return_value = '1' as get_attrib_mock would now work as get_attrib after patching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM