mercredi 24 mars 2021

Raise error when calling a non-existent method of a mocked method of a mock created from `spec`

from unittest import mock
class A:
    def f(self): pass
m = mock.MagicMock(spec_set=A)
m.f.called_once()  # I want this to fail
Out[42]: <MagicMock name='mock.f.called_once()' id='140326790593792'>

I have made a mistake in my unit test and called called_once instead of assert_called_once on a mocked method. The call results in a new MagicMock instance, so the test was passing and not checking what I inteneded to check -- if the method was invoked. Is there a way to make mock fail if the method is not defined, when the mock is created from spec_set? It's like I want spec_set to be applied all the way down to the method mocks themselves.

Aucun commentaire:

Enregistrer un commentaire