正确的方法python mock __init __()方法返回一个假类

时间:2012-03-12 15:34:36

标签: python unit-testing mocking

试图模拟调用pyazure库进行django测试,但我无法弄清楚如何模拟PyAzure类构造函数,以免它导致TypeError。有没有更好的方法来模拟生成连接对象的访问库?

我尝试过的除None之外的任何东西都会产生TypeError,这意味着我甚至无法开始使用实际返回值测试任何PyAzure连接方法。使用mock替换假类的最佳方法是什么?

测试错误:

======================================================================
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'
----------------------------------------------------------------------

tests.py:

 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

models.py

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

1 个答案:

答案 0 :(得分:11)

您似乎对__init__()所做的事情存在误解。其目的是初始化先前已创建的实例。 __init__()的第一个参数是self,它是实例,因此您可以看到它在调用__init__()时已经分配。

__new__()之前调用方法__init__()来创建实际实例。但是,我认为用模拟类替换整个类会更容易,而不是模仿单个方法。