Mocked object returning another Mocked object

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jul 13 06:09:22 EDT 2012


Greetings Fellow Group,


I'd need your help to fix some issues I have with my unitary tests.
I'm trying to bring them to the next level by Mocking equipments 
controlled by my application.

Despite me reading http://www.voidspace.org.uk/python/mock/index.html I 
cannot figure out how to do the following :

I have an App object with the 'target' attribute. This target is 
controlling a piece of hardware. The hardware itself holds a software, 
hence the target object having an 'api' attribute. I hope I make sense.

So basically I'd like

self.target.api.<anyMethod>()

to return a Mocked object (Mocking an Api object response).



** Question **

I do I make *all* method calls return a specifik Mock?

target = Mock()

result = target.api.start() # I'd like result to be a Mock I defined 
with the 'returnCode' attribute

print result.returnCode
1

But I would like to do it for any method of api (the list is huge, 
setting each of them is not an option I think) so that in the end,

result = target.api.start()
result = target.api.stop()
result = target.api.reset()
result = target.api.loadSw()

return all the same Mock object (with 'returnCode')


Any idea ?

Cheers,

JM





More information about the Python-list mailing list