Django 测试 Post 请求耗尽数据
Django Testing Post request exhaust the data
class SampleTest(APITestCase):
def setUp(self):
self.id = 1
self.api_url = 'api/check_customers'
self.token ='##############'
def test_check_customer(self):
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token)
response = self.client.post(self.api_url, data={'id': self.id})
self.assertEqual(response.status_code, status.HTTP_200_OK)
当我测试这段代码时,我得到了错误消息,我已经为检查参数是否为空设置了错误消息,比如
{'message': 'id is empty', 'status': 'failed'}
我检查这个的方式是在视图中
class Customer(APIView):
def post(self, request, *args, **kwargs):
if "id" not in request.data:
content = {"status": "failed",
"message": "id is empty"}
return Response(content, status=STATUS.HTTP_400_BAD_REQUEST)
我正在使用 DRF 3.3.0 和 Django 1.9
response = self.client.post(self.api_url, data={'id': self.id}, format='json')
这很好用。默认格式类型是多部分,在传递字典时必须是 json
class SampleTest(APITestCase):
def setUp(self):
self.id = 1
self.api_url = 'api/check_customers'
self.token ='##############'
def test_check_customer(self):
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token)
response = self.client.post(self.api_url, data={'id': self.id})
self.assertEqual(response.status_code, status.HTTP_200_OK)
当我测试这段代码时,我得到了错误消息,我已经为检查参数是否为空设置了错误消息,比如
{'message': 'id is empty', 'status': 'failed'}
我检查这个的方式是在视图中
class Customer(APIView):
def post(self, request, *args, **kwargs):
if "id" not in request.data:
content = {"status": "failed",
"message": "id is empty"}
return Response(content, status=STATUS.HTTP_400_BAD_REQUEST)
我正在使用 DRF 3.3.0 和 Django 1.9
response = self.client.post(self.api_url, data={'id': self.id}, format='json')
这很好用。默认格式类型是多部分,在传递字典时必须是 json