python - 如何模拟在类属性中调用的函数?
问题描述
我有一个具有调用函数的属性的类:
users.mixins.py
:
from django.contrib import messages
from ..utils.services import redirect_to_previous
class AdminRightsMixin:
def dispatch(self, request, *args, **kwargs):
if not request.user.is_admin:
messages.warning(
request, 'You must have administrator rights to view this page.'
)
redirect_to_previous(request)
return super().dispatch(request, *args, **kwargs)
我想测试那个redirect_to_previous
叫做,但我不知道如何在我的测试中模拟它。
例如:
@patch.object(AdminRightsMixin.dispatch, 'redirect_to_previous')
def test_admin_mixin(self, mock_class):
// code
结果是AttributeError: <function AdminRightsMixin.dispatch at 0x000001D54FDB6430> does not have the attribute 'redirect_to_previous'
解决方案
推荐阅读
- python - 为我的 Mac 安装 psycopg2 时出错
- python - python setup.py build ctypes.CDLL:无法打开共享对象文件:没有这样的文件或目录
- ios - Xcode:点击自动填充后关闭键盘
- memory-management - gSOAP 客户端和服务器(尤其是服务器)中的简单内存管理
- java - Java“最简单”的地图实现/链接地图(NOT LinkedHashMap)
- python - 如何使 itertools.product() 随机
- flutter - 为什么 TextField 返回 null
- python-3.x - 最长公共子串python
- javascript - 如何在 React 中同步两个具有不同显示的下拉列表?
- snowflake-task - 如何计算最近一周的 AVG 时间戳