title: proper way python mock __init__() method that returns a fake class
body: <p>Trying to mock out calls to pyazure library for django testing, but I can't figure out how to mock out the PyAzure class constructor so that it doesn't cause a TypeError.  Is there a better way to approach mocking out an access library that generates a connection object?</p>
  
  <p>Anything I've tried other than None generates a TypeError, which means I can't really even begin to test any of the PyAzure connection methods with actual return values.  What is the best way to replace a working class with a fake class using mock?</p>
  
  <p>Test Error:</p>
  
  <pre><code>======================================================================
  ERROR: test_management_certificate_connect (azure_cloud.tests.ViewsTest)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/Users/bschott/Source/django-nimbis/apps/azure_cloud/tests.py", line 107, in test_management_certificate_connect
  self.cert1.connect()
  File "/Users/bschott/Source/django-nimbis/apps/azure_cloud/models.py", line 242, in connect 
      subscription_id=self.subscription.subscription_id)
  TypeError: __init__() should return None, not 'FakeAzure'
  ----------------------------------------------------------------------
  </code></pre>
  
  <p>tests.py:</p>
  
  <pre><code> class ViewsTest(TestCase):
      def setUp(self):
      ...
          self.cert1 = ManagementCertificate.objects.create(
              name="cert1",
              subscription=self.subscription1,
              management_cert=File(open(__file__), "cert1.pem"),
              owner=self.user1)
      ...
  
      class FakeAzure(object):
          """ testing class for azure """
          def list_services(self):
              return ['service1', 'service2', 'service3']
          def list_storages(self):
              return ['storage1', 'storage2', 'storage3']
  
      @mock.patch.object(pyazure.PyAzure, '__init__')
      def test_management_certificate_connect(self, mock_pyazure_init):
          mock_pyazure_init.return_value = self.FakeAzure()
          self.cert1.connect()
          assert mock_pyazure_init.called
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>class ManagementCertificate(models.Model):
  
      # support connection caching to azure
      _cached_connection = None
  
      def connect(self):
          """
          Connect to the management interface using these credentials.
          """
          if not self._cached_connection:
              self._cached_connection = pyazure.PyAzure(
                  management_cert_path=self.management_cert.path,
                  subscription_id=self.subscription.subscription_id)
              logging.debug(self._cached_connection)
          return self._cached_connection
  </code></pre>