我有想要测试的功能,允许用户在主页上添加记录,然后在保存的页面中查看记录 - 这在运行应用程序时有效。
最初运行下面的测试时,用户已登录,但当浏览器 url 指向 /saved 时,用户已变成 AnonymousUser。
是否有一个原因?下面是我的代码。
测试:
def test_viewing_logged_in_users_saved_records(self):
# A user logs in
self.client.login(username = 'testuser1', email='testuser1@test.com', password = 'testuser1password')
self.browser.get(self.live_server_url)
# set up our POST data - keys and values are strings
# and post to home URL
response = self.client.post('/',
{'title': TestCase3.title
})
# The user is redirected to the new unit test display page
self.assertRedirects(response, 'unittestcase/hLdQg28/')
# Proceeds to the page where they can see their saved records
self.browser.get(self.live_server_url + '/saved')
# The user can view the tests that they have saved
body = self.browser.find_element_by_tag_name('body')
self.assertIn(TestCase3.title, body.text)
看法:
def home_post(request):
logging.warning('In home_post')
logging.warning(request.user)
if request.method == 'POST':
if request.user.is_authenticated():
....
def saved(request):
logging.warning('In saved')
logging.warning(request.user)
if request.user.is_authenticated():
....
记录:
WARNING:root:In home_post
WARNING:root:testuser1
WARNING:root:In saved
WARNING:root:AnonymousUser