Python mockito - Mocking a class that is created from the function being checked

I lose when writing a test case for the UserCompanyRateLimitValidation class . It's hard for me to mock a class that is created from within this class.

class UserCompanyRateLimitValidation:
    def __init__(self, user_public_key):
        self.adapter = UserAdapter(user_public_key)
        container = self.adapter.get_user_company_rate_limit()
        super(UserCompanyRateLimitValidation, self).__init__(container,\
                                            UserCompanyRateLimitValidation.TYPE)

I need to check this class. I wrote a test case like this. I tried to make fun of the UserAdapter class, but I cannot do it completely.

def test_case_1():
   self.user_public_key = 'TEST_USER_PUBLIC_KEY_XXXXXX1234567890XXXXX'
   UserAdapter_mock = mock(UserAdapter)
   when(UserAdapter_mock).get_user_company_rate_limit().\
                                          thenReturn(get_fake_container_object())

   self.test_obj = UserCompanyRateLimitValidation(self.user_public_key)

Here, if you see that I was mocking the get_user_company_rate_limit () call from the function being tested, container = self.adapter.get_user_company_rate_limit()  but I still can’t figure out how I can mock this call,

 self.adapter = UserAdapter(user_public_key)
+4
source share
1

, .

Python . UserCompanyRateLimitValidation - 'invoking' UserAdapter(user_public_key). "", UserAdapter_mock.

, . :

when(module_declaring_UserAdapter)\
    .UserAdapter(self.user_public_key)\
    .thenReturn(UserAdapter_mock)

module_declaring_UserAdapter.UserAdapter(self.user_public_key) UserAdapter_mock.

: https://code.google.com/p/mockito-python/wiki/Stubbing#Modules

, module_declaring_UserAdapter, - , from ... import .... , , UserCompanyRateLimitValidation.

+2

Source: https://habr.com/ru/post/1529172/


All Articles