【发布时间】:2016-11-17 18:04:05
【问题描述】:
我正在使用djangorestframework==3.3.3 和Django==1.9.4
我有一个测试,我想检查查询参数是否正确处理。
class TestResourceView(APITestCase):
def test_view_process_query_params_correctly(self):
client = APIClient()
client.login(username='<username>', password='password')
response = client.get('/api/v2/resource/1/users/?fields=first_name;last_name')
self.assertEqual(response.status_code, 200)
# .... rest of the test ....
在我看来,我输入print 语句只是为了查看查询参数是否被正确解析,但我得到空查询字典:
class Resource(APIView):
def get(self, request):
query_params = request.query_params
print('Printing query params')
print(query_params)
# .... rest of the code ....
def post(self, request):
query_params = request.query_params
print('Printing query params')
print(query_params)
# .... rest of the code ....
运行测试时在终端中产生结果:
Printing query params
<QueryDict: {}>
同时,如果我像这样测试post 请求:
response = client.post('/api/v2/resource/1/users/?fields=first_name;last_name')
我得到的参数解析不正确:
Printing query params
<QueryDict: {'last_name': [''], 'fields': ['first_name']}>
APIClient的正确使用方法是什么?或者这仍然是一个错误?因为已经有类似的issue
【问题讨论】:
标签: django django-rest-framework django-testing