Make unittest.mock.create_autospec resilient against AttributeError on original object

This commit is contained in:
Michael Foord 2012-04-13 17:39:16 +01:00
parent 899ee613f7
commit 656319e58d
2 changed files with 29 additions and 2 deletions

View file

@ -651,6 +651,29 @@ class SpecSignatureTest(unittest.TestCase):
mock.f.assert_called_with(3, 4)
def test_skip_attributeerrors(self):
class Raiser(object):
def __get__(self, obj, type=None):
if obj is None:
raise AttributeError('Can only be accessed via an instance')
class RaiserClass(object):
raiser = Raiser()
@staticmethod
def existing(a, b):
return a + b
s = create_autospec(RaiserClass)
self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3))
s.existing(1, 2)
self.assertRaises(AttributeError, lambda: s.nonexisting)
# check we can fetch the raiser attribute and it has no spec
obj = s.raiser
obj.foo, obj.bar
def test_signature_class(self):
class Foo(object):
def __init__(self, a, b=3):