我在针对Django REST API框架编写测试时遇到了一些问题。
我在MacOSV.10.10.3上安装了Python2.7.6,Django==1.6.5,djangorestframework==3.1.1。我正在尝试编写一些针对Django REST框架的测试。
我有一个API的虚拟测试视图:
class TestApiView(APIView):
def get(self, request, format=None):
return Response(status=status.HTTP_404_NOT_FOUND)
当我叫它的时候,它工作得很好:
(venv)bash-3.2# curl -H "Accept: application/json; indent=2" -i --request GET "http://127.0.0.1:8000/api/v1/client/test/"
HTTP/1.0 404 NOT FOUND
Date: Thu, 30 Apr 2015 18:35:09 GMT
Server: WSGIServer/0.1 Python/2.7.6
Vary: Accept, Cookie
Allow: GET, HEAD, OPTIONS
但当我想针对这个视图启动一个测试时,它引发了异常。
我的测试代码:
from rest_framework.test import APIClient
client = APIClient()
print client.get('/api/v1/client/test/', format='json')
此代码引发异常:
Internal Server Error: /api/v1/client/test/
Traceback (most recent call last):
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
form = data.serializer
AttributeError: 'NoneType' object has no attribute 'serializer'
Traceback (most recent call last):
File "/Users/user/dev/managerv2/tests.py", line 20, in <module>
print client.get('/api/v1/client/test/', format='json')
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 160, in get
response = super(APIClient, self).get(path, data=data, **extra)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 86, in get
return self.generic('GET', path, **r)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/compat.py", line 222, in generic
return self.request(**r)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 157, in request
return super(APIClient, self).request(**kwargs)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 109, in request
request = super(APIRequestFactory, self).request(**kwargs)
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/test/client.py", line 444, in request
six.reraise(*exc_info)
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
form = data.serializer
AttributeError: 'NoneType' object has no attribute 'serializer'
基于MixSimult.ListMaldMin的视图,几乎与同一个问题一样——它工作正常,但在测试过程中引发异常。
视图定义:
class ListTestView(mixins.ListModelMixin, generics.GenericAPIView):
pagination_class = paginators.StandardResultsSetPagination
def get(self, request, format=None, *args, **kwargs):
self.serializer_class = TestListSerializer
self.queryset = Client.objects.all()
return self.list(request, *args, **kwargs)
从测试客户端调用此视图:
print client.get('/api/v1/client/list/', format='json')
引发此异常:
Internal Server Error: /api/v1/client/list/
Traceback (most recent call last):
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
form = data.serializer
AttributeError: 'OrderedDict' object has no attribute 'serializer'
Traceback (most recent call last):
File "/Users/user/dev/managerv2/tests.py", line 22, in <module>
print client.get('/api/v1/client/golden_state/list/1/', format='json')
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 160, in get
response = super(APIClient, self).get(path, data=data, **extra)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 86, in get
return self.generic('GET', path, **r)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/compat.py", line 222, in generic
return self.request(**r)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 157, in request
return super(APIClient, self).request(**kwargs)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/test.py", line 109, in request
request = super(APIRequestFactory, self).request(**kwargs)
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/test/client.py", line 444, in request
six.reraise(*exc_info)
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response
response = response.render()
File "/Users/user/dev/venv/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/response.py", line 59, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/Users/user/dev/venv/lib/python2.7/site-packages/rest_framework/renderers.py", line 333, in render
form = data.serializer
AttributeError: 'OrderedDict' object has no attribute 'serializer'
有什么建议吗?非常感谢!
最佳答案
删除print语句,这应该有效。响应表示似乎假定存在序列化程序。