[英]How to mock decorator in django unit test?
我正在嘗試為我的視圖編寫簡單的單元測試。 我有一個裝飾器,它通過發送 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.