我有以下代码和测试,现在想“间谍”一次外部检索是否真的被调用过。我该怎么办?
class AbstractExternal(metaclass=ABCMeta):
@abstractmethod
def retrieve(self, user):
pass
class Do:
def __init__(self, func):
self.external = func
def get_name(self, username):
user = self.external.retrieve(username)
return '{} {}'.format(
user.first_name,
user.second_name,
)
def test_get_name(monkeypatch):
monkeypatch.setattr(AbstractExternal, 'retrieve', lambda self: SimpleNamespace(
attributes={
'first_name': 'given_name',
'second_name': 'family_name',
}
))
do = Do(AbstractExternal)
data = do.get_name('Egon')
assert data == 'given_name family_name'