mercredi 4 mai 2016

How to get mock.patch to error if a method is called with extra kwargs?

I want my tests to catch the situation of a method signature changing.

Say I have the following class:

class Adder(object):
    def sum(self, a, b):
        return a + b

If I had a mock of Adder called mock_adder, how could I make it throw an exception if some code-under-test called mock_adder.sum(1, 3, 4), i.e. with extra, unsupported parameters?

I've tried creating a patch passing an instance of adder as the spec_set parameter, and also (separately) setting autospec=True, but mock still doesn't throw an error when the call occurs.

Aucun commentaire:

Enregistrer un commentaire