Closed GoogleCodeExporter closed 8 years ago
Why not just use patch or patch.object to attach your override methods to the
class?
Original comment by fuzzyman
on 14 Nov 2013 at 11:20
Can you post an example of how the snippet above could be rewritten using
patch/patch.object? I'm probably missing something but as far as I can tell
from the docs and the examples, patch is useful for replacing an original
attribute/method with a fake one. Here I do want to call the original a and b
methods so that their side effects take place (print something, set
self.x/self.y attributes). The only reason for using mock here is to fake
everything *except* 'a' and 'b' (e.g. a mock 'other' method).
Original comment by George.S...@fieldaware.com
on 15 Nov 2013 at 8:41
mock.patch.object would require you to override individual methods on the
class. To fake everything *except* a few methods is trickier (although you
could do it with a factory function that iterates over all methods on a class
and replaces them using patch.object).
Do you have a proposed api for this?
Original comment by fuzzyman
on 18 Nov 2013 at 12:53
I just realized a surprisingly simple way that works in my case: override the
desired methods in a class that inherits from Mock (or MagicMock):
class Foo(object):
def a(self): ...
def b(self, n): ...
def c(self, x, y): ...
class FooMock(MagicMock):
def a(self):
self.x = 0
def b(self, n):
self.y = 1 + n
m = FooMock()
m.a() # calls FooMock.a
print m.x # 0
m.b(2) # calls FooMock.b
print m.y # 3
print m.c(3,4) # mock object
print m.other() # mock object
So it doesn't seem necessary to introduce a new parameter after all.
Cheers,
George
Original comment by George.S...@fieldaware.com
on 18 Nov 2013 at 9:53
Ok, I'm glad that works for you.
Original comment by fuzzyman
on 19 Nov 2013 at 3:22
Original issue reported on code.google.com by
George.S...@fieldaware.com
on 8 Nov 2013 at 11:54