python - mock __init__(self, ...): TypeError: super(type, obj): obj must be an instance or subtype of type -


i try mock constructor of class https://stackoverflow.com/a/17950141/633961

class mockedhttpresponse(django.http.response.httpresponsebase):     def check(self, *args, **kwargs):         status=kwargs.get('status')         if status none , 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 

usage in test:

def test_assert_http_response_status__with_self_mocking(self):     django.http import httpresponse     mock.patch('django.http.response.httpresponse', testutils.mockedhttpresponse):         httpresponse(status=200) 

but exception:

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 instance or subtype of type 

how can mock class , modify __init__() method?

i can't sure since i'm not expert on how mock works, unless it's doing impressive gymnastics, you've statically bound original django.http.httpresponse object httpresponse within test module scope (from django.http import httpresponse). unless mock dynamically modifying locals() \ globals() within context manager patch on aliased references patch target, no amount of patching on module impact httpresponse object using in test. think @ least 1 step need take reference class module explicitly within patch context manager, though looks use with mock.patch('django.http.response.httpresponse', testutils.mockedhttpresponse) httpresponse: desired result. think may want use new_callable=testutils.mockedhttpresponse kwarg patch.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -