我有一個客戶經理一個Django模型,覆蓋create
方法還可以節省一些相關的對象:你如何模擬Django中的RelatedManager方法?
class CustomManager(models.Manager):
def create(self, amount, user, description):
txn = self.get_query_set().create(user, description)
txn.budget_transactions.create(amount)
return txn
我的問題是:我怎麼嘲笑調用txn.budget_transactions.create
引發異常?
txn
對象的budget_transactions
屬性是django.db.models.fields.related.RelatedManager
的一個實例。使用mock.patch
來模擬這個類不能工作,因爲它是動態聲明的 - 它不能直接導入。
有誰知道如何做到這一點?
你使用模擬庫嗎?你想使用補丁上下文管理器或修飾器嗎? – santiagobasulto
如果能解決這個問題,我很樂意使用模擬庫。正如我上面所述,由於RelatedManager類的動態聲明,以傳統方式使用'mock.patch'不起作用。 – DavidWinterbottom