我正在尝试使用 Python 模拟库来模拟 Django 模型上的方法:
# file: tasks.py
def delete_ads(user):
# works fine and return a list of 4 MagicMock objects
ads = Classifieds.objects.filter(
user=user
)
# file: tests.py
def test_delete_ads():
return_list = [MagicMock(name='1'), MagicMock(name='2'), MagicMock(name='3'), MagicMock(name='4')]
with patch('user.tasks.Classifieds') as classified_mock:
classified_mock.objects.filter.return_value = return_value
上面的代码工作正常,但在我将代码更改为以下代码后开始返回单个 MagicMock 对象:
# file: tasks.py
def delete_ads(user):
# works fine and return a list of 4 MagicMock objects
ads = Classifieds.objects.filter(
user=user
).order_by('-added')
# file: tests.py
def test_delete_ads():
return_list = [MagicMock(name='1'), MagicMock(name='2'), MagicMock(name='3'), MagicMock(name='4')]
with patch('user.tasks.Classifieds') as classified_mock:
classified_mock.objects.filter.order_by.return_value = return_value
无论如何,即使我在 Django 模型上链接方法调用,我也可以正确地做到这一点?