Django Testing Post请求耗尽数据

时间:2017-07-30 14:11:07

标签: django-1.9 django-rest-framework

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

1 个答案:

答案 0 :(得分:2)

response = self.client.post(self.api_url, data={'id': self.id}, format='json')

这很好用。默认格式类型是multipart,在传递字典时必须是json