gpt4 book ai didi

python - Django Rest Framework 测试用例问题 : 'HttpResponseNotAllowed' object has no attribute 'data'

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:12 24 4
gpt4 key购买 nike

我的测试文件代码:

from rest_framework import status
from rest_framework.test import APITestCase

class CategoryTests(APITestCase):

def test_create_create(self):

url = '/category/add/'
data = {"name":"Sports","description":"get live updates here"}
response = self.client.post(url, data, format='json')
self.assertEqual(response.data, data)

我得到的错误:

Traceback (most recent call last):
File "/Users/test/webapp/apps/core/tests.py", line 16, in test_create_create
self.assertEqual(response.data, data)
AttributeError: 'HttpResponseNotAllowed' object has no attribute 'data'

事实上,测试甚至没有调用确切的 api 语句(我在 api 代码中使用调试语句检查过)。请让我知道可能出了什么问题,或者您需要这方面的更多信息。

最佳答案

尝试使用 DRF 扩展测试客户端:

from rest_framework import status
from rest_framework.test import APITestCase, APIClient

class CategoryTests(APITestCase):
client = APIClient()

def test_create_create(self):

url = '/category/add/'
data = {"name":"Sports","description":"get live updates here"}
response = self.client.post(url, data, format='json')
self.assertEquals(response.data, data)

关于python - Django Rest Framework 测试用例问题 : 'HttpResponseNotAllowed' object has no attribute 'data' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com