derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

MagicMock 'protocol' methods workaround for 0.7.2? #122

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. m = magicMock()
2. m[1]  # not a magicMock

This is fixed in rev 481, but is there a way to simulate it?  just wait until 
0.8?

Original issue reported on code.google.com by gregg.l...@lolapps.com on 6 Oct 2011 at 9:50

GoogleCodeExporter commented 9 years ago
This should do it:

    m.__getitem__ = MagicMock() 

Original comment by fuzzyman on 6 Oct 2011 at 10:14

GoogleCodeExporter commented 9 years ago
Fuzzyman, I was unclear in the statement of the problem!  I was hoping for a 
solution that would go to an arbitrary depth (i.e. m[1][1][1]).  I appreciate 
the answer, and this is the best workaround I have come up with so far:

import mock

def _gettable(*args,**kwargs):
    M = mock.MagicMock()
    M.__getitem__ = _gettable
    return M

m = mock.MagicMock()
m.__getitem__ = _gettable

Original comment by gregg.l...@lolapps.com on 7 Oct 2011 at 2:50

GoogleCodeExporter commented 9 years ago
Ah, indeed. Yours is an interesting and clever solution. The best solution of 
course is to use 0.8.

Original comment by fuzzyman on 7 Oct 2011 at 3:05