Python mockito - Mocking a class which is being instantiated from the testable function
Question: I am bit lost while writing the test case for
**UserCompanyRateLimitValidation** class. I am finding difficulty in mocking
the class which is being instantiated from inside 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 have to test this class. I have written test case something like this. I
have tried to mock the UserAdapter class but I am not able to do so
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 I have mocked get_user_company_rate_limit() call from the
testable function, `container = self.adapter.get_user_company_rate_limit()`
but I am still not able to figure out the way in which I can mock this call,

    
    
     self.adapter = UserAdapter(user_public_key)
    


Answer: It is quite simple if you know the trick.

Creating an object in Python is very much like a function call to the class
object. `UserCompanyRateLimitValidation` is 'invoking'
`UserAdapter(user_public_key)`. You want to stub the return value of that
'call' to return `UserAdapter_mock`.

You can stub this like you would stub a function in a module. The line you're
missing is:

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

After that, calling
`module_declaring_UserAdapter.UserAdapter(self.user_public_key)` will return
`UserAdapter_mock`.

Here's the link to the section in the manual:
<https://code.google.com/p/mockito-python/wiki/Stubbing#Modules>

You have to be careful to choose the right `module_declaring_UserAdapter`, due
to the way the `from ... import ...` statement works. From your code, I'd say
you have to pick the module in which `UserCompanyRateLimitValidation` is
declared.

