【发布时间】:2012-02-25 21:02:51
【问题描述】:
我在使用 django 的客户端类测试我的应用程序时尝试访问 request.user 对象。
from django.test import TestCase
from django.test.client import Client
class SomeTestCase(TestCase):
def setUp(self):
self.client = Client()
self.client.login( username="foo", password="bar")
def test_one(self):
response = self.client.get("/my_profile/")
self.fail( response.request.user )
这显然会失败,但它失败了,因为 response.request 没有用户属性。
AttributeError: 'dict' object has no attribute 'user'
有没有办法从客户端实例访问用户对象?我想这样做是为了检查某些测试用户的数据是否正确呈现。我可以尝试通过在设置期间设置所有必要信息来解决这个问题,但在我这样做之前,我想检查一下我是否只是错过了一些东西。
【问题讨论】:
标签: django testing client request