TypeError: 'Token' object is not callable

TypeError: 'Token' object is not callable

当我用 pytest 测试 test_delivery.py 时,出现错误:“TypeError:'Token' 对象不可调用”。

我正在将 pytest 与 Django REST 一起使用。从我看到的示例来看,代码似乎是正确的。我错过了什么?

basic.py

import pytest

@pytest.fixture
def api_client():
   from rest_framework.test import APIClient
   return APIClient()

@pytest.fixture
def test_password():
   return 'Str0ng-test-pa$$'

import uuid

@pytest.fixture
def create_user(db, django_user_model, test_password):
   def make_user(**kwargs):
       kwargs['password'] = test_password
       if 'username' not in kwargs:
           kwargs['username'] = str(uuid.uuid4())
       return django_user_model.objects.create_user(**kwargs)
   return make_user


from rest_framework.authtoken.models import Token

@pytest.fixture
def get_or_create_token(db, create_user):
   user = create_user()
   token, _ = Token.objects.get_or_create(user=user)
   return token

test_delivery.py

import pytest

from django.urls import reverse
from tests.utils.basic import api_client, get_or_create_token, create_user, test_password
from rest_framework.authtoken.models import Token


@pytest.mark.django_db
def test_unauthorized_request(api_client):
   url = reverse('deliveries-list')
   response = api_client.get(url)
   assert response.status_code == 401

@pytest.mark.django_db
def test_authorized_request(api_client, get_or_create_token):
   url = reverse('deliveries-list')
   token = get_or_create_token()
   api_client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
   response = api_client.get(url)
   assert response.status_code == 200

[更新] 错误详情

======================================================= FAILURES =======================================================
_______________________________________________ test_authorized_request ________________________________________________

api_client = <rest_framework.test.APIClient object at 0x7f1ee5e0fa00>
get_or_create_token = <Token: 1771cc20278aed83af8e09646286edd1ef8cb7b7>

    @pytest.mark.django_db
    def test_authorized_request(api_client, get_or_create_token):
       url = reverse('deliveries-list')
>      token = get_or_create_token()
E      TypeError: 'Token' object is not callable

tests/test_delivery/test_delivery.py:17: TypeError

这是对我有用的新 test_authorized_request。我更改了创建令牌的方式。

test_delivery.py

@pytest.mark.django_db
def test_authorized_request(api_client, get_or_create_token): 
   user = User.objects.create_user('test', 'user@example.com', 'Strong-test-pass')
   token = Token.objects.create(user=user)
   url = reverse('deliveries-list')
   api_client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
   response = api_client.get(url)
   assert response.status_code == 200