You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Possible Solution
I was able to solve this issue on my own by monkey patching the test client by defining a function like
from django.contrib.sessions.backends.db import SessionStore
def _new_build_request(self, *args, **kwargs) -> Mock:
"""Method to be monkey patched into the TestClient to add session store to the request mock"""
mock = self._old_build_request(*args, **kwargs)
mock.session = SessionStore()
return mock
and then using this new function to replace the _build_request function in my TestClient instance like
AttributeError: Mock object has no attribute 'session'
This error is raised when using TestClient to test a login endpoint that uses
django.contrib.auth.login
because the mock request object as defined here https://github.com/vitalik/django-ninja/blob/master/ninja/testing/client.py#L128-L138 is missing a session attribute.Possible Solution
I was able to solve this issue on my own by monkey patching the test client by defining a function like
and then using this new function to replace the
_build_request
function in my TestClient instance likeMaybe a better solution would be to use a SessionStore mock?
The text was updated successfully, but these errors were encountered: