I made an attempt to test on a views.py in which the file has one line using request.session['key']
values.
def updateaccout(request):
username = request.session['username']
profile = Account.objects.get(username=username)
return render(request, "user/profile .html",profile )
At the test.py file I have tried sets.
def test_updateaccout(self):
self.client.login(username='john', password='johnpassword')
session = self.client.session
session['username'] = 'john'
session.save()
c = Client()
data = {'username' : 'john'}
response = c.post('/account', data,follow=True)
self.assertEqual(response.status_code,200)
ERROR: test_updateaccout (user.tests.UsersTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ubuntu/cn331project/user/tests.py", line 57, in test_updateaccout
response = c.post('/login/account', data,follow=True)
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/test/client.py", line 741, in post
response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/test/client.py", line 405, in post
secure=secure, **extra)
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/test/client.py", line 470, in generic
return self.request(**r)
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/test/client.py", line 709, in request
self.check_exception(response)
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/test/client.py", line 571, in check_exception
raise exc_value
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/cn331project/user/views.py", line 50, in updateaccout
username = request.session['username']
File "/home/ubuntu/.venv/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 65, in __getitem__
return self._session[key]
KeyError: 'username'
Another one that I tried to set is to add self._session ['username'] = 'john'
but it says AttributeError: 'UsersTestCase' object has no attribute '_session'
how can I do to solve it?
thank you
Aucun commentaire:
Enregistrer un commentaire