如何使用Django的assertJSONEqual来验证返回JsonResponse视图的响应 [英] How to use Django's assertJSONEqual to verify response of view returning JsonResponse

查看:805
本文介绍了如何使用Django的assertJSONEqual来验证返回JsonResponse视图的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Python 3.4和Django 1.7。我有一个视图返回 JsonResponse

  def add_item_to_collection(request):
#(...)
return JsonResponse({'status':'success'})

我想验证该视图是否返回正确使用单元测试的回复:

  class AddItemToCollectionTest(TestCase):

def test_success_when_not_added_before(self):
response = self.client.post('/ add-item-to-collection')
self.assertEqual(response.status_code,200)
self.assertJSONEqual(response.content,{'状态':'success'})

然而 assertJSONEqual() code> line引发了一个例外:

 错误
追溯(最近最近的呼叫):
文件E:\Projects\collecthub\app\collecthub\collecting\ test.py,第148行,在test_success_when_added_before
self.assertJSONEqual(response.content,{'status':'OK'})
文件E:\Projects\collecthub\venv\\ \\ lib\site- packages\django\test\testcases.py,第675行,assertJSONEqual
data = json.loads(raw)
文件C:\Python34\ Lib \json\__init __。py,第312行,加载
s .__类__.__ name__))
TypeError:JSON对象必须是str,而不是'bytes'

当响应包含JSON时,什么是检查响应内容的正确方法?为什么我尝试比较原始值时的类型错误?code> assertJSONEqual()

解决方案

看起来您正在使用Python 3,因此您需要在传递之前将 response.content 转换为UTF-8编码的字符串 self.assertJSONEqual

  class AddItemToCollectionTest(TestCase):

def test_success_when_not_added_before(self):
response = self.client.post('/ add-item-to-collection')
self.assertEqual(response.status_code,200)
self.assertJSONEqual(
str(response.content,encoding ='utf8'),
{'status':'success'}

如果您想同时支持Python 2.7和Python 3,请使用兼容性库,django 船只

  from __future__ import unicode_literals 
from django.utils导入6

class AddItemToCollectionTest(TestCase):

def test_success_when_not_added_before(self):
response = self.client.post('/ add-item-to-收集')
self.assertEqual(response.status_code,200)

response_content = response.content
如果six.PY3:
response_content = str(response_content,encoding ='utf8')

self.assertJSONEqual(
response_content,
{'status':'success'}


I'm using Python 3.4 and Django 1.7. I have a view returning JsonResponse.

def add_item_to_collection(request):
    #(...)
    return JsonResponse({'status':'success'})

I want to verify if that view returns correct response using unit test:

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(response.content, {'status': 'success'})

However the assertJSONEqual() line raises an exception:

Error
Traceback (most recent call last):
  File "E:\Projects\collecthub\app\collecthub\collecting\tests.py", line 148, in test_success_when_added_before
    self.assertJSONEqual(response.content, {'status': 'OK'})
  File "E:\Projects\collecthub\venv\lib\site-packages\django\test\testcases.py", line 675, in assertJSONEqual
    data = json.loads(raw)
  File "C:\Python34\Lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

What is thet correct way of checking content of response, when response contains JSON? Why i get type error when i try to compare raw value agains a dict in assertJSONEqual() ?

解决方案

It looks like you're working with Python 3 so you'll need to turn response.content into a UTF-8 encoded string before passing it to self.assertJSONEqual:

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'),
            {'status': 'success'}
        )

If you want to simultaneously support both Python 2.7 and Python 3, use the six compatibility library that django ships with:

from __future__ import unicode_literals
from django.utils import six

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)

        response_content = response.content
        if six.PY3:
            response_content = str(response_content, encoding='utf8')

        self.assertJSONEqual(
            response_content,
            {'status': 'success'}
        )

这篇关于如何使用Django的assertJSONEqual来验证返回JsonResponse视图的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆