python - 如何在 django 单元测试中模拟装饰器?
问题描述
我正在尝试为我的观点编写简单的单元测试。我有一个装饰器,它通过发送 http 请求进行身份验证。如何模拟我的视图装饰器并运行单元测试?
我的意见.py
@method_decorator(authentication_decorator, name='post')
class AddBlogView(CreateAPIView):
serializer_class = BlogSerializer
解决方案
试试这个(为您的应用程序和文件名调整代码):
from mock import patch
patch('app.decorators.authentication_decorator', lambda x: x).start() # This line must come before other imports
from app.views import AddBlogView
# Test AddBlogView
推荐阅读
- google-cloud-platform - Logsink 到 bigquery 分区不起作用
- virtual-memory - 限制进程的虚拟地址范围?
- php - 如何使用 PHPDoc?
- sql - 是否有一种将可空列级联到引用表的非触发方式?
- javascript - 无法解决承诺拒绝并发送数组作为响应
- forms - 如何在 ms 访问中更新单个表单上的多个表?
- ruby-on-rails - 您的 Ruby 版本是 2.6.3,但您的 Gemfile 指定了 ~> 2.3.1
- powershell - 无法正确获取 xml 节点的最后一个节点
- javascript - 指数图动画 P5js 画布
- jquery - DatePicker - 周五下午 3 点后,周一禁用