New issue 14 by alan.franzoni: Mox should handle class attributes raising
an AttributeError
http://code.google.com/p/pymox/issues/detail?id=14
Hello,
while using some classes from some external libraries, i realized that if
the parent class uses
zope.interface classProvides and the child does not, I get an
AttributeError while trying to use it
with CreateMock()
this happens because a "rogue" class attribute persists in the class dir()
output (as it is inherited
by the parent class), but it raises AttributeError whenever it is called on
the child class.
A small example which employs a metaclass is attached, so you don't need
installing
zope.interface if you don't want to.
This happens because Mox uses dir() and then something like
getattr(class_to_mock, method),
which breaks in the event an AttributeError is raised. But raising
AttributeError is *the* right way
to do this in python, if you just want customize your object behaviour.
IMHO just using the failsafe getattr(class_to_mock, method, NO_ATTR)
signature is the best
choice; it will break if other errors are raised, just trapping
AttributeError. Of course, in such a
case, the mocked object should NOT expose such attribute.
a patch, with tests, is attached.
Attachments:
test_classprovides.py 419 bytes
mox-attributeerror.patch 2.2 KB
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
of course the title should read "...handle MISSING class..."