2017-07-30 40 views
0
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) 

当我测试此代码,我得到一个我已经设置了检查参数像Django的测试后要求排放数据

{'message': 'id is empty', 'status': 'failed'} 

的emptyness和方式的错误信息我检查,这是在意见

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

+0

https://github.com/ericleunghk?tab=overview&from=2016-12-01&to=2016-12-31 此链接指出相同的问题 – Sagar

回答

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

这工作正常。默认格式类型是多部分,必须是json,而传递字典