【发布时间】:2022-09-28 04:00:07
【问题描述】:
我需要在 mockito 中模拟单个函数(不是类成员,也不是导入模块的一部分)。我读到 mockito 模拟是可调用的,所以我可以使用 __call__() 函数,但不幸的是它对我不起作用。有一个旧的question 关于它在 3 年前被问到,我想从那时起可能有一些变化(所以请不要将此问题作为该旧线程的重复来解决)。
这是示例代码(取自那个旧线程):
import os
import unittest
from mockito import when, verify
def interesting_function():
os.system(\'mkdir some_dir\')
another_function()
def another_function():
print(\'Done\')
class InterestingFunctionTests(unittest.TestCase):
def test_interesting_function(self):
when(another_function).__call__().thenReturn()
interesting_function()
verify(another_function).__call__()
它应该可以工作我收到以下错误:
mockito.verification.VerificationError:
Wanted but not invoked:
__call__()
Instead got:
Nothing
如何在 mockito 中模拟和验证单个函数?
我很感激任何帮助。
标签: python unit-testing mockito