在测试时使用 django 中的会话对象?
我创建了一个包含三个应用程序的小型 django 项目,现在正在为其中一个应用程序编写测试。我需要在不同的视图和不同的模板之间传递一些信息,但该信息不应该对用户可见。我的第一次尝试是将此信息作为 HTML 表单中的隐藏字段传递,但后来有人向我指出,这并没有使其完全不可见。因此,我将这些信息存储在 request.session 字典中,一切顺利。
也就是说,我的问题是在测试时出现的。根据 django 文档(http://docs.djangoproject.com/en/1.2/topics/testing/),当您在测试期间必须修改会话字典时,您应该首先将其存储在变量中,修改它,然后保存变量。
所以我的测试代码是这样的:
class Test_Atacar(TestCase):
fixtures = ["testBase.json"]
def test_attack_without_troops(self):
red_player = Player.objects.get(color=RED)
self.failUnless(red_player != None)
session = self.client.session
session["player_id"] = red_player.id
session.save()
response = self.client.get("/espectador/sadfxc/", follow=True)
但是当我运行 python manage.py 测试时,我得到一个 AttributeError,说 dict 没有属性 save()。 我在其他地方读到(http://code.djangoproject.com/ticket/11475),我应该尝试在操作会话之前对任何其他 URL 执行 self.client.get ,以便它将成为“真正的”会话,但是我不断收到相同的 AttributeError 。
I've created a small django project with three applications, and I'm now writing tests for one of them. I needed to pass some information between differente views and differents templates,but that information should not be visible to the user. My first attempt was to pass this informatio as hidden fields in a HTML form, but then it was pointed to me that that did not make it completely invisible. So, I stored this information in the request.session dictionary and it went all right.
That said, my problem arised while testing. According to the django documentation (http://docs.djangoproject.com/en/1.2/topics/testing/) when you have to modify the session dictionary during testing you should first store it in a variable, modify it, and then save the variable.
So my testing code is something like this:
class Test_Atacar(TestCase):
fixtures = ["testBase.json"]
def test_attack_without_troops(self):
red_player = Player.objects.get(color=RED)
self.failUnless(red_player != None)
session = self.client.session
session["player_id"] = red_player.id
session.save()
response = self.client.get("/espectador/sadfxc/", follow=True)
But when I run the python manage.py test, I get an AttributeError, saying that dict, has no attribute save().
I read somewhere else (http://code.djangoproject.com/ticket/11475) that I should try doing a self.client.get to any other URL BEFORE manipulating the session so that it would become a "real" session, but I kept getting the same AttributeError.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你在测试期间必须修改会话字典时,你应该首先将其存储在一个变量中,修改它,然后保存该变量
这行意味着如果你想对会话的某些部分进行一些更改变量,不要将它们直接放入会话中。将数据存储在变量中,在该变量中进行更改,然后将该变量放入会话字典中。会话就像任何其他字典一样。
when you have to modify the session dictionary during testing you should first store it in a variable, modify it, and then save the variable
This line means that if you want to make some changes into some of the session variables, do not make them directly into session. Store the data in the variable, make changes in that variable and then put that variable into session dictionary. session is like any other dictionary.
@anand我知道这很奇怪,但它确实有效。为了让它工作,除了不直接操作变量之外,我还必须做一个 self.client.get("/dummy/") ,其中 dummy 是使用虚拟视图的 URL。该视图仅修改作为参数获取的请求的属性。老实说,我不知道幕后发生了什么让这一切成功
@anand I know it's weird but it indeeds work. What I had to do to make it work, besides not manipulating directly the variable was to make a self.client.get("/dummy/") where dummy is an URL that uses a dummy view. This view only modifies the attribute of the request it gets as argument. Honestly, I don't know what goes on behind the scenes that makes this work