I am using RequestFactory
in a Django test, and I can't find the right way to access the session variable, and I'm getting the following error when I try
self.factory._session["zip_id"]
or self.factory.session["zip_id"]
.
======================================================================
ERROR: test_middleware (dj_geo.tests.IPToZipMiddleWareTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\dj_site_test\dj_geo\tests.py", line 36, in test_middleware
assert self.factory._session["zip_id"] != None
AttributeError: 'RequestFactory' object has no attribute '_session'
----------------------------------------------------------------------
@override_settings(MIDDLEWARE_CLASSES=(
'dj_geo.middleware.IPToZipMiddleWare'
))
class IPToZipMiddleWareTest(TestCase):
def test_middleware(self):
Zipcode.syncdb()
assert Zipcode.objects.all().count() > 0
self.factory = RequestFactory()
self.request = self.factory.get('/', {}, **{'REMOTE_ADDR':'108.31.178.99'})
assert self.factory._session["zip_id"] != None
assert self.factory._session["zip_id"] != ""
TypeError: __init__() missing 1 required positional argument: 'get_response'
on Django 4.0 with this solution. I solved it bymiddleware = SessionMiddleware(lambda x: x)
Although I don't know, if it is really correct solution. – Dewitt