测试Django vi时模拟模块

2024-10-05 14:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个自定义函数来验证请求。我试着在测试过程中模仿这个模块,但到目前为止还没有运气

这是我的观点

from auth_utils import authenticate_request, UnauthenticatedRequest

def my_view(request):
    try:
        authenticate_request(request)
    except UnauthenticatedRequest:
        return Http404()
    return render(request, 'ok.html', {'status': 'ok'})

在测试中,我试图模拟authenticate_request,这样它就不会产生错误

^{pr2}$

不能让它工作。有什么建议吗?在

谢谢,Python2.7,Django1.8。在


Tags: 模块函数fromimportauthreturn过程request
1条回答
网友
1楼 · 发布于 2024-10-05 14:26:04

您需要模拟函数authenticate_request,它是在哪里导入的,而不是定义在哪里。在

因此,例如,如果my_view是在myapp/views.py中定义的,那么authenticate_request将被导入myapp.views。所以你可以这样称呼:

@mock.patch('myapp.views.authenticate_request', side_effect=None)

相关问题 更多 >