【发布时间】:2019-10-06 16:15:58
【问题描述】:
我正在尝试测试我的函数是否在测试中被调用,但我得到:
AttributeError: 'function' object has no attribute 'assert_called_once'
我没有嘲笑这个正确的,所以请你帮我找出为什么我的模拟在这种情况下不起作用。我有正确的方法来模拟这个函数。
我已经尝试过这个 -> create_autospec 来解决 AttributeError,但没有运气。
代码示例:
class MyClass:
def __init__(self):
self._data = {}
def a(self, value):
self._data = value
@pytest.fixture
def my_fixture():
return MyClass()
@pytest.mark.asyncio
async def test_random_function(my_fixture, mocker):
s = mocker.patch('path.module.a',
my_fixture.a)
await random_function()
s.assert_called_once()
【问题讨论】:
标签: python unit-testing mocking pytest attributeerror