我想模拟一个将对象保存到数据库的方法。我试图Assert失败时的重试行为。在第一次尝试时,我想提出一个例外。在第二次尝试中,我希望调用底层方法,即保存到数据库中。
例如
# production code
import time
class Model(Base):
def save(self):
for i in range(3):
try:
super().save()
except Exception as e:
time.sleep(1)
else:
break
# test code
from unittest import mock
def test_saving():
model = Model()
with mock.patch("module.Model.save", ...): # ... <- what to put here
model.save()
# assert the model was persisted
暂无答案!
目前还没有任何答案,快来回答吧!