简体   繁体   English

Mocking a function called inside another function inside a class python

[英]Mocking a function called inside another function inside a class python

I need to mock a function(this_function()), that is called inside another function(receive()), inside a function blueprint, inside a class A. How can I do it?我需要在 function 蓝图中,在 class A 中模拟一个函数(this_function()),该函数在另一个函数(receive())中调用。我该怎么做? I create a mock for this_function() in the file where i have all tests.我在我有所有测试的文件中为 this_function() 创建了一个模拟。

class A: 
       def blueprint(): 
           @route("/", method = ["POST"])
           async def receive(): 
                  ....
              test = await this_function()

I created a mock function in the same file as my test:我在与我的测试相同的文件中创建了一个模拟 function :

def mock_this_function(): 
   return ..

class Tests(unittest.TestCase): 
   @patch("path.to.classA.this_function")
    def test(mock_this_function):

This_function is defined in a helper.py module. this_function 在 helper.py 模块中定义。

I'm not able to connect/use mock_this_function.我无法连接/使用 mock_this_function。 It always connect me with this_function... Any solution?它总是将我与 this_function 联系起来......有什么解决方案吗? any Tips how to properly mock it?任何提示如何正确模拟它?

Try this path in your mock: " path.to.classA.this_function " instead, you are probably having the wrong path.在你的模拟中尝试这条路径: path.to.classA.this_function ,你可能有错误的路径。 I mean, it doesnt matter how nested your function is.我的意思是,你的 function 的嵌套程度并不重要。 If it is imported, then you just need to refer to the mock path properly, which is very well described in the link provided by @HallerPatrick.如果是导入的,则只需正确引用模拟路径即可,@HallerPatrick 提供的链接中对此进行了很好的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Mocking function 在嵌套函数中调用 - Python Mocking function called inside nested functions Python-使用side_effect模拟出在类的init内部调用的函数 - Python - Mocking out a function called inside the init of a class using side_effect Python测试:模拟在另一个函数中导​​入并使用的函数 - Python testing: mocking a function that's imported AND used inside another funciton python为什么在一个类中定义的一个函数不能在同一类的另一个函数内部调用? - python why one function defined in a class cant be called inside another function in same class? 在 python 中的另一个 class 中调用 class function - calling of class function inside another class in python Python:调用另一个函数内部的函数时出现问题 - Python: Problems calling a function which is inside another called function Python模拟-如何在函数内部修补变量 - Python Mocking - How to patch a variable inside a function 类内的函数未调用 - Function inside class not being called (Python) Mocking 在另一个方法内部调用的方法的返回值 - (Python) Mocking the return value of a method called inside another method Python:在另一个文件的路由内的类中调用函数 - Python: calling function inside a class inside a route from another file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM