jeudi 23 juin 2016

Mocking __mul__ and __rmul__ in Python

I am attempting to mock the following line of code:

surface['width'] = '{:~}'.format(self._width * self._scaling_factor_x)

I pass _width and _scaling_factor_x as objects of class unittest.MagicMock. Afterwards, I replace the __mul__ method of _width with:

def multiplicateMocks(self, other):
    return MagicMock(name = self.name + '*' + other.name)

This code, however, does not work. The second argument, other, is not passed. Testing in a Python interpreter session shows that having replaced __mul__ with:

def newMul(*args):
    return args

and calling it, there is indeed only a single argument present - the lefthand operand.

Can a proper multiplication be attempted?

Aucun commentaire:

Enregistrer un commentaire