首页 > 解决方案 > Django TypeError:force_authenticate()缺少1个必需的位置参数:'self'

问题描述

测试用例setUp()首先创建一个用户并尝试强制对用户进行身份验证,但发生有线错误......

类型错误:force_authenticate() 缺少 1 个必需的位置参数:'self'

class PrivateUserApiTest(TestCase):
    """ Test API that require authentication """

    def setUp(self):
        self.user = create_user(
            email="test@test.com",
            password="testpass",
            name="testName"
        )
        print(self.user)
        self.client = APIClient
        self.client.force_authenticate(user=self.user) # <-- 

标签: pythondjangodjango-modelsdjango-rest-frameworktdd

解决方案


如前所述@BhavyaParikh,而不是这样:

self.client = APIClient

做这个:

self.client = APIClient()

推荐阅读