From 9751c637d6a58fe38a85668993f7c0079181fd0e Mon Sep 17 00:00:00 2001 From: smiddlek Date: Thu, 19 Aug 2010 17:29:46 +0000 Subject: MockObject should have the __name__ param by default. When stubbing with MockAnything, force the __name__ param git-svn-id: http://pymox.googlecode.com/svn/trunk@57 b1010a0a-674b-0410-b734-77272b80c875 --- mox.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mox.py b/mox.py index d6be02a..d2711fe 100755 --- a/mox.py +++ b/mox.py @@ -316,6 +316,7 @@ class Mox(object): stub = self.CreateMock(attr_to_replace) else: stub = self.CreateMockAnything(description='Stub for %s' % attr_to_replace) + stub.__name__ = attr_name self.stubs.Set(obj, attr_name, stub) @@ -543,14 +544,12 @@ class MockObject(MockAnything, object): self._known_vars = set() self._class_to_mock = class_to_mock try: - self._description = class_to_mock.__name__ - # If class_to_mock is a mock itself, then we'll get an UnknownMethodCall - # error here from the underlying call to __getattr__('__name__') - except (UnknownMethodCallError, AttributeError): - try: + if inspect.isclass(self._class_to_mock): + self._description = class_to_mock.__name__ + else: self._description = type(class_to_mock).__name__ - except AttributeError: - pass + except Exception: + pass for method in dir(class_to_mock): attr = getattr(class_to_mock, method) @@ -770,6 +769,11 @@ class MockObject(MockAnything, object): return self._class_to_mock + @property + def __name__(self): + """Return the name that is being mocked.""" + return self._description + class _MockObjectFactory(MockObject): """A MockObjectFactory creates mocks and verifies __init__ params. -- cgit v1.2.1