title: How to mock class attribute instantiated in __init__?
body: <p>I'm trying to mock the "self.api.friends.get" method in VKAuth class:</p>
  
  <pre><code>import vk
  
  class VKAuth(object):
      def __init__(self, access_token, user):
          self.session = vk.Session(access_token = access_token)
          self.api = vk.API(self.session)
  
      def follow(self):
          vk_friends = self.api.friends.get()
  </code></pre>
  
  <p>from the test module test_views.py:</p>
  
  <pre><code>from mock import patch
  from ..auth_backends.vk_backend import VKAuth
  
  class AddUsersToList(TestCase):
      def test_auth_vk(self, mock_get):
           ... etc ...
          auth_token = 'ceeecdfe0eb4bf68ceeecdfe0eb4bf68ceeecdfe0eb4bf68652530774ced6cbc8cba0'
          token = user.auth_token.key
          self.client.defaults['HTTP_AUTHORIZATION'] = 'Token {}'.format(token)
          with patch.object(accounts.auth_backends.vk_backend.VKAuth, 'api'): #point where we're mocking
              response = self.client.post(reverse('auth-social', kwargs=dict(backend='vk')), dict(access_token=auth_token), follow=True)
  </code></pre>
  
  <p>having created instance of VKAuth class during the post call to 'auth-social' above in SNView class-based view:</p>
  
  <pre><code>class SNView(generics.GenericAPIView):
      serializer_class = serializers.AuthSocialSerializer
      permission_classes = (rest_permissions.IsAuthenticated)
  
      def post(self, request, backend, *args, **kwargs):
          s = self.get_serializer(data=request.DATA)
  
          if s.is_valid():
              auth_backends = {
                  'vk': VKAuth,
                  'facebook': FBAuth
              }
  
              if backend in auth_backends:
                  auth_backend = auth_backends[backend](access_token=s.data['access_token'], user=self.request.user)
  </code></pre>
  
  <p>And I get an error:</p>
  
  <pre><code>AttributeError: &lt;class 'accounts.auth_backends.vk_backend.VKAuth' doens't have the attribute 'api'
  </code></pre>
  
  <p><strong>What should I write istead of the current patch.object to reach api.friends.get and mock it?</strong> </p>
  
  <p><strong>UPD:</strong></p>
  
  <p>To be more precise, I want some equivalent of:</p>
  
  <pre><code>    auth_token = 'ceeecdfe0eb4bf68ceeecdfe0eb4bf68ceeecdfe0eb4bf68652530774ced6cbc8cba0'
      user = User.objects.get(id = 2)
      vk_auth = VKAuth(auth_token, user)
  
      vk_ids=[111111,2222222,3333333,44444444]
      vk_auth.authenticate()
      vk_auth.api.friends = MagicMock(name='get', return_value=None)
      vk_auth.api.friends.get = MagicMock(name='get', return_value=vk_ids)
      data = vk_auth.follow()
  </code></pre>
  
  <p>but mock it the moment before we make request to the django-rest-framework api via self.client.post().</p>
  
  <p>Thank you!</p>