I have a set of unit tests which will repeatedly be using a certain cooperator class Rental
that I want to mock, with the same arguments passed every time. To make this easier, I want to make a subclass of mock.Mock and pass the arguments on creation. Here's the code:
class RentalMock(Mock):
def __call__(self, *args, **kwargs):
super(RentalMock, self).__call__(*args, spec_set=Rental, **kwargs)
self.get_points.return_value=0
return self
The problem is, when I instantiate this class, that override has no visible effect. And trying to override it here also doesn't work.
> a = RentalMock()
> a.get_points()
<RentalMock name='mock.get_points' id='4447663888'>
> a.get_points.return_value = 0
> a.get_points()
<RentalMock name='mock.get_points' id='4447663888'>
> a.configure_mock(**{"get_points.return_value":0})
> a.get_points()
<RentalMock name='mock.get_points' id='4447663888'>
I'm thoroughly confused. I've tried three methods, all taken directly from the docs, and none seem to work. When I pass these arguments directly to an instance of Mock, they work fine. What am I missing?
You are overriding __call__
when it looks like you want to override __init__
. Subclassing can often get involved, especially with something as already intricate as Mock. Perhaps a factory function would be simpler.