Django Unitest Checking Value Of Template Variable

痴心易碎 提交于 2019-12-11 07:49:19

问题


) Suppose I have {{registered}} variable in template. I wrote a piece of test:

def nice_test():
     response = self.client.post(reverse('app:register;), {'username': 'dupa'}

and there I want to check value of variable registered in response. How to do it ?


回答1:


The response from the test client has access to the template context used.

def nice_test():
     response = self.client.post(reverse('app:register'), {'username': 'dupa'})
     self.assertEqual(response.context['registered'], '<expected value>')

Here is a reference to the official documentation: https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.Response.context

class Response
...
context
The template Context instance that was used to render the template that produced the response content.



来源:https://stackoverflow.com/questions/27190359/django-unitest-checking-value-of-template-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!