1
我這裏有一個奇怪的問題通過...Django的test.Client已經登錄,但作爲AnonymousUser的意見
我試圖測試@login_required意見,所以我寫了這個代碼:
from django.test import TestCase
from django.test import Client
from .models import SiteEcommerce
from .models import Client as TestDbClient
from .models import MyCustomEmailUser
class DataBaseTestCase(TestCase):
def __init__(self, *args, **kwargs):
super(DataBaseTestCase, self).__init__(*args, **kwargs)
self.c = Client() # instantiate the Django test client
self.user = MyCustomEmailUser.objects.all()[0]
def test_method(self):
"""
Test of the settings page.
"""
self.c.login(username=self.user.email, password=self.user.password)
print self.user.is_authenticated()
response = self.client.get('/main/settings/%s' % self.website.idsite)
self.assertEqual(response.status_code, 200)
觀點稱作settings_view
我在哪裏打印request.user
的問題是,在測試print self.user.is_authenticated()
給True
但在request.user
查看返回AnonymousUser
謝謝您的回答@Alasdair,但我得到這個錯誤:'與update_fields保存不影響任何rows.' – farhawa
我不知道什麼會導致該錯誤。這可能是因爲你已經使用'__init__'而不是'setUp'。如果更改不能解決問題,那麼您需要提供更多信息,包括完整的回溯。 – Alasdair