[英]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.