vendredi 27 mai 2016

How to mock a function with specific parameter and return value that calls another function in python unit test?

i am new to python unit testing. Want to mock a function that calls other functions.

Here is my function that i want to mock

def has_groups(self, group_names):
        auth_user_id = AuthUser.get_by_email(self.userEmail).id
        auth_user_groups = AuthUserGroups.get_group_by_user_id(auth_user_id)
        for auth_user_group in auth_user_groups:
            if auth_user_group.group.name in group_names:
                return True
        return False

has_groups should return True only when it get's 'Admin' as parameter.

Here is my test

def my_test(self):
    uid = self.auth_user.get_by_email = Mock(return_value=73)
    groups = AuthUserGroups.get_group_by_user_id = Mock(uid, return_value='Admin')
    self.auth_user.has_groups = Mock(groups, return_value=True)

but it's not working fine. I will appreciate if anyone help me

Can i use patch decorator for this and how?

Aucun commentaire:

Enregistrer un commentaire