mock __init __(self,...):TypeError:super(type,obj):obj必须是类型的实例或子类型

时间:2014-07-01 14:18:03

标签: python django unit-testing mocking

我尝试模拟像https://stackoverflow.com/a/17950141/633961

这样的类的构造函数
class MockedHttpResponse(django.http.response.HttpResponseBase):
    def check(self, *args, **kwargs):
        status=kwargs.get('status')
        if status is None and self.status_code==200:
            return # default 
        if not status==self.status_code:
            raise self.AssertionErrorStatusCode('%s!=%s' % (self.status_code, status))

    def __init__(self, *args, **kwargs):
        self.check(*args, **kwargs)
        django.http.response.HttpResponseBase.__init__(self, *args, **kwargs)

    class AssertionErrorStatusCode(AssertionError):
        pass

测试中的用法:

def test_assert_http_response_status__with_self_mocking(self):
    from django.http import HttpResponse
    with mock.patch('django.http.response.HttpResponse', testutils.MockedHttpResponse):
        HttpResponse(status=200)

但我得到了这个例外:

Traceback (most recent call last):
  File "/home/foo_eins_di514/src/djangotools/djangotools/tests/unit/utils/test_testutils.py", line 129, in test_assert_http_response_status__with_self_mocking
    HttpResponse(status=200)
  File "/home/foo_eins_di514/lib/python2.7/site-packages/django/http/response.py", line 258, in __init__
    super(HttpResponse, self).__init__(*args, **kwargs)
TypeError: super(type, obj): obj must be an instance or subtype of type

如何模拟一个类并修改其__init__()方法?

1 个答案:

答案 0 :(得分:0)

我无法肯定地说,因为我不是mock如何运作的专家,但除非它做了一些非常令人印象深刻的体操,否则你已经静态约束测试模块范围(django.http.HttpResponse)内的原始HttpResponse对象from django.http import HttpResponse。除非mock动态修改上下文管理器中的locals() \ globals()以修补对修补程序目标的别名引用,否则模块本身的修补量不会影响HttpResponse您在测试中使用的对象。我认为您需要采取的至少一个步骤是在patch上下文管理器中显式引用该模块中的类,尽管看起来您也可以使用with mock.patch('django.http.response.HttpResponse', testutils.MockedHttpResponse) as HttpResponse:来获得所需的结果。我想您可能还想将new_callable=testutils.MockedHttpResponse用作patch的kwarg。