I have a class with an attribute that calls a function:
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)
I want to test that redirect_to_previous
is called, but I don't know how to mock this in my test.
For example:
@patch.object(AdminRightsMixin.dispatch, 'redirect_to_previous')
def test_admin_mixin(self, mock_class):
// code
Results in AttributeError: <function AdminRightsMixin.dispatch at 0x000001D54FDB6430> does not have the attribute 'redirect_to_previous'