derwiki-adroll / mock

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

Need to override a method of a Mock wrapped by a class #193

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
Please help with the issue below.
Suppose I have two classes:

class A():
def __init__(self):
self.a = 1
def use(self, b):
return self.a * b

class B(A):
def __init__(self):
super(B, self).__init__()

Next in the test, I create a Mock wrapped by class B:
API = B()
mock = MagicMock(wraps = API)

Now I want to override the use() method in the mock. How can I do that ?
I mean, I want to modify the mock to call instead of the original use() to be 
something like:

def use(self, b):
self.a = 2
return self.a * b

Thanks

Original issue reported on code.google.com by dim...@gmail.com on 20 Dec 2012 at 7:23

GoogleCodeExporter commented 9 years ago
I think you want (inside use) to do API.a = 2

If your "use" function does not take self as a parameter and accesses "API" 
directly, then you can do mock.use = use.

Otherwise you probably need to subclass B - so you can override use - and wrap 
that instead. Alternatively use patch to put your temporary use implementation 
onto the B class.

Original comment by fuzzyman on 19 Mar 2013 at 12:27