mardi 24 mars 2015

TypeError: unbound method when trying to mock a classmethod

This scripts fails:



import mock

class MyClass(object):

@classmethod
def my_method(cls):
print('my_method')

def mocked_method(cls):
print('I want this method to get called')

with mock.patch.object(MyClass, 'my_method', mocked_method):
MyClass.my_method()


Exception:



Traceback (most recent call last):
File "/home/foo/tmp/test_mocking_classmethod.py", line 14, in <module>
MyClass.my_method()
TypeError: unbound method mocked_method() must be called with MyClass instance as first argument (got nothing instead)

Aucun commentaire:

Enregistrer un commentaire