AttributeError:<bound method =“”manager.get =“”of =“”<django.db.models.manager.manager =“”

时间:2017-06-20 23:36:19

标签: python django unit-testing django-rest-framework

=“”

我在单元测试中没有经过实验,我收到的错误如下,我无法修复,我将不胜感激。感谢

这是返回的错误:

`{Null, {{{0, 1}, {0, 2}, {0, 3}, {0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 
4}, {5, 4}, {6, 4}, {7, 4}, {8, 4}, {9, 4}, {10, 4}, {0, 5}, {0, 
6}, {0, 7}, {0, 8}, {0, 9}, {0, 10}}}}`

这是代码:

    ======================================================================
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'

单元测试

class ExpiringTokenAuthentication(TokenAuthentication):
    """
    Extends token auth with inactivity expiration mechanism.
    """
    model = ExpiringToken

    def authenticate_credentials(self, key):

        try:
            token = self.model.objects.get(key=key)
        except self.model.DoesNotExist:
            raise exceptions.AuthenticationFailed('Invalid token')

        if not token.user.is_active:
            raise exceptions.AuthenticationFailed('Invalid user')

        if token.has_expired():
            raise exceptions.AuthenticationFailed('Token has expired')

2 个答案:

答案 0 :(得分:0)

问题是:您正试图通过.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')

答案 1 :(得分:0)

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

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')