我正在使用django.test.client.Client來測試當用戶登錄時是否顯示一些文本。但是,我的客戶端對象不會' t似乎讓我登錄。爲什麼django.test.client.Client不讓我登錄
該測試通過,如果手動完成與Firefox,但不完成與客戶端對象。
class Test(TestCase):
def test_view(self):
user.set_password(password)
user.save()
client = self.client
# I thought a more manual way would work, but no luck
# client.post('/login', {'username':user.username, 'password':password})
login_successful = client.login(username=user.username, password=password)
# this assert passes
self.assertTrue(login_successful)
response = client.get("/path", follow=True)
#whether follow=True or not doesn't seem to work
self.assertContains(response, "needle")
當我打印響應它返回被隱藏的登錄表單:
{% if not request.user.is_authenticated %}
... form ...
{% endif %}
當我運行ipython manage.py shell
這證實。
問題似乎是客戶端對象沒有保持會話認證。
看着這個我不確定問題在哪裏。你能明確表明客戶端正在註銷嗎?如果客戶端已經登錄,那麼'client.session!= {}'您可以使用它來顯示客戶端在何處登出。 – tdedecko 2010-05-21 04:13:42
這肯定沒有幫助你,對不起,但我從今天起就有同樣的問題。我不知道改變了什麼。 – Fred 2010-05-21 12:47:13