IndexError:在测试用例中列出索引超出范围

时间:2017-11-16 22:13:37

标签: django python-3.x django-rest-framework python-unittest django-tests

我有奇怪的问题

我有下一个代码使用TesCase和APIClient获取带令牌的请求

    class Profile(TestCase):
        fixtures = ['profiles.json']

        def setUp(self):
             self.client = APIClient()
             self.user = Profile.objects.get(username='some-username')
             self.token = Token.objects.create(user=self.user)
             self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

        def test_profile(self):
                response = self.client.get('/api/some-username/profile/')               
                print(response.status_code)
                print(response.data)`

授权进展顺利,但我收到错误。如果我使用带有头文件(令牌)的requests.get来开发django服务器,那么我会收到正确的json响应

运行此测试时

完全回溯:

Traceback (most recent call last):
  File "/home/work/project/profiles/tests.py", line 103, in test_user_profile
    response = self.client.get('/api/some-username/profile/')
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 291, in get
    response = super(APIClient, self).get(path, data=data, **extra)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 208, in get
    return self.generic('GET', path, **r)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 237, in generic
    method, path, data, content_type, secure, **extra)
  File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 416, in generic
    return self.request(**r)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 288, in request
    return super(APIClient, self).request(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 240, in request
    request = super(APIRequestFactory, self).request(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 501, in request
    six.reraise(*exc_info)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 686, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework_tracking/mixins.py", line 69, in handle_exception
    response = super(BaseLoggingMixin, self).handle_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/generics.py", line 254, in get
    return self.retrieve(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 58, in retrieve
    return Response(serializer.data)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 533, in data
    ret = super(Serializer, self).data
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 500, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/home/work/project/profiles/serializers.py", line 10, in to_representation
    return institute.all()[0].slug
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 289, in __getitem__
    return list(qs)[0]
IndexError: list index out of range

怎么知道是什么问题?

0 个答案:

没有答案