AttributeError:<绑定方法经理.get的<Django.db.型号.经理,经理

2024-09-30 12:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在单元测试方面不是很有经验,我得到了下面的错误,我无法修复,我希望能得到一些帮助。谢谢

这是返回的错误:

    ======================================================================
ERROR: test_authenticate_credentials_for_inactive_user (apps.authentication.tests.test_authentication.AuthenticateCredentialsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/site-packages/mock/mock.py", line 1297, in patched
    arg = patching.__enter__()
  File "/usr/local/site-packages/mock/mock.py", line 1369, in __enter__
    original, local = self.get_original()
  File "/usr/local/site-packages/mock/mock.py", line 1343, in get_original
    "%s does not have the attribute %r" % (target, name)
AttributeError: <bound method Manager.get of <django.db.models.manager.Manager object at 0x00000000015bd168>> does not have the attribute 'has_expired'

这是代码:

^{pr2}$

单元测试:

class AuthenticateCredentialsTest(TestCase):

    def setUp(self):
        self.ExpiringTokenAuth = ExpiringTokenAuthentication()


    @patch('apps.authentication.authentication.ExpiringTokenAuthentication.model.objects.get')
    @patch('apps.authentication.authentication.ExpiringTokenAuthentication.model.objects.get.user.is_active')
    @patch('apps.authentication.authentication.ExpiringTokenAuthentication.model.objects.get.has_expired')
    def test_authenticate_credentials_for_inactive_user(self, mock_token, active_user, expired_token):
        active_user.return_value = True
        expired_token.return_value = False
        with self.assertRaises(exceptions.AuthenticationFailed) as ea:
            self.ExpiringTokenAuth.authenticate_credentials('valid key')

Tags: appstestselfgetauthenticationusrlocalsite
2条回答

你可以在不同的测试中分开每个案例。例如:

class AuthenticateCredentialsTest(TestCase):

def setUp(self):
    self.ExpiringTokenAuth = ExpiringTokenAuthentication()
    self.token = ExpiringToken.objects.create(key="valid_key")

def tearDown(self):
    del self.ExpiringTokenAuth
    del self.token

def test_for_non_existent_token(self):
    with self.assertRaises(exceptions.AuthenticationFailed) as ea:
        self.ExpiringTokenAuth.authenticate_credentials('invalid_key')

def test_for_user_inactive(self):
    user = <UserModel>.objects.create(is_active=False, **params)  # Create your own inactive user
    self.token.user = user
    self.token.save()

    with self.assertRaises(exceptions.AuthenticationFailed) as ea:
        self.ExpiringTokenAuth.authenticate_credentials('valid_key')

def test_for_has_expired(self):
    self.token.expired = True  # Make the method has_expired return True
    self.token.save()

    with self.assertRaises(exceptions.AuthenticationFailed) as ea:
        self.ExpiringTokenAuth.authenticate_credentials('valid_key')

问题是:您试图通过.get()方法来路径返回一个对象,这不是它的工作方式。您需要自行修补实例:

@patch('apps.authentication.authentication.ExpiringTokenAuthentication.model.objects.get')
@patch('apps.authentication.authentication.token.user.is_active')
@patch('apps.authentication.authentication.token.has_expired')
def test_authenticate_credentials_for_inactive_user(self, mock_token, active_user, expired_token):
    active_user.return_value = True
    expired_token.return_value = False
    with self.assertRaises(exceptions.AuthenticationFailed) as ea:
        self.ExpiringTokenAuth.authenticate_credentials('valid key')

相关问题 更多 >

    热门问题