MarkBerlin78 / pymox

Automatically exported from code.google.com/p/pymox
Apache License 2.0
0 stars 0 forks source link

Mox should handle class attributes raising an AttributeError #14

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by alan.fra...@gmail.com on 10 Oct 2009 at 3:04

Attachments:

GoogleCodeExporter commented 9 years ago
of course the title should read "...handle MISSING class..."

Original comment by alan.fra...@gmail.com on 10 Oct 2009 at 3:05

GoogleCodeExporter commented 9 years ago
This is especially useful for when mocking out Django models that have custom 
fields. Would like to see this get incorporated, although not sure how active 
this project is since this was posted in 2009 and there has been no response to 
it since.

Original comment by paulcava...@gmail.com on 21 Oct 2011 at 6:17

GoogleCodeExporter commented 9 years ago
Ah, I totally missed this patch.  I'll take a look at it this weekend, 
hopefully.

Original comment by steve.mi...@gmail.com on 21 Oct 2011 at 11:52

GoogleCodeExporter commented 9 years ago
Hey Steve,

Sorry to be so pushy, but is there something more I could do to help move this 
along? I know that it's difficult to maintain an open source project like this, 
but if there's something extra I could do, don't hesitate to ask.

Thanks,

-Paul

Original comment by paulcava...@gmail.com on 25 Oct 2011 at 4:21

GoogleCodeExporter commented 9 years ago
Don't worry, I need the motivation - I've been slacking on this patch.  I'm 
traveling for work now, but the patch looks reasonable (just looking at the 
diffs), but the tests look a little sparse.  I'm in a conference all day 
tomorrow, and I'm sure there will be post conference get togethers, but I'll 
try and patch this and add more tests so I'm confident this code does what I 
expect it to do on Thursday.

If you want to provide a patch with more tests, that'd be really helpful.

Original comment by steve.mi...@gmail.com on 26 Oct 2011 at 6:07