0

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?

Jacob Kopczynski
  • 392
  • 3
  • 13

1 Answers1

0

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • ...yup, I misread the subclassing example I was using. Mock is weird enough that using call instead of init to affect the guts of it seemed plausible – Jacob Kopczynski May 01 '17 at 04:30