【发布时间】:2021-06-09 00:56:38
【问题描述】:
我需要模拟一个函数(this_function()),它在另一个函数(receive())、函数蓝图、A 类中调用。我该怎么做? 我在包含所有测试的文件中为 this_function() 创建了一个模拟。
class A:
def blueprint():
@route("/", method = ["POST"])
async def receive():
....
test = await this_function()
我在与我的测试相同的文件中创建了一个模拟函数:
def mock_this_function():
return ..
class Tests(unittest.TestCase):
@patch("path.to.classA.this_function")
def test(mock_this_function):
this_function 在 helper.py 模块中定义。
我无法连接/使用 mock_this_function。它总是将我与 this_function 联系起来......有什么解决方案吗?任何提示如何正确模拟它?
【问题讨论】:
-
我认为很难复制你的例子并给你正确的答案。项目结构是什么?
this_function函数在哪里定义?如果我不得不猜测你可能无法理解 where 来模拟。这可能会对您有所帮助:docs.python.org/3/library/unittest.mock.html#where-to-patch