Open GoogleCodeExporter opened 8 years ago
Coping with mocks that are called with mutable arguments is a known issue. In
the documentation I suggest a couple of different ways of dealing with it:
http://www.voidspace.org.uk/python/mock/examples.html#coping-with-mutable-arguments
This comes up rarely enough that I don't think I'm going to build support for
it into mock, but the documentation does suggest a few different ways you can
deal with it.
Original comment by fuzzyman
on 11 Sep 2012 at 10:18
Thanks for the quick reply. The docs did help me find a workaround :)
I have had a quick look in the code of Mock. Couldn't this be fixed by adding
the following code to the top of the function CallableMixin._mock_call?
if self.deepcopy_calllist:
args = deepcopy(args)
kwargs = deepcopy(kwargs)
it will also need to handle the kwarg 'deepcopy_calllist' in __init__. Then you
should be able to use Mock(deepcopy_calllist=True) to handle this problem.
Original comment by erling.b...@gmail.com
on 11 Sep 2012 at 11:33
Original comment by fuzzyman
on 11 Sep 2012 at 11:34
I also ran into this issue today.
Here's one API suggestion for this feature. What if Mock accepted a "save_arg"
argument that defaulted to the identity function `lambda x: x`?
This would let people easily insert the flavor of copying that they want, which
could be shallow or deep (e.g. lambda x: x.copy()).
The implementation should also be easy because I believe it would just be a
matter of wrapping an expression with a call to self.save_arg() at the
appropriate spot. Thanks.
Original comment by chris.je...@gmail.com
on 29 Oct 2014 at 1:00
Original issue reported on code.google.com by
erling.b...@gmail.com
on 11 Sep 2012 at 8:22