![](/img/trans.png)
[英]Python unittest mock method which called in another module work error
[英]Python unittest patch method imported and called in another method
我正在嘗試修補在另一個模塊中定義和調用的方法。 模塊名稱是 authentication.py,它有方法 get_customer。
from authorization import get_customer
class ResolveCustomer():
def resolve_customer(info1, info2):
cust = get_customer(info1.context, info2.context)
return cust
import unittest
from unittest.mock import patch
class TestSchema(unittest.TestCase):
def test_schema(self):
obj = ResolveCustomer()
with patch("authorization.get_customer") as mock_get_customer:
info1 = {}
info2 = {}
obj.resolve_customer(info1, info2)
self.assertTrue(mock_get_customer.called)
當我運行測試時,我得到的結果是 AttributeError: 'dict' object has no attribute 'context'
這意味着它正在嘗試調用真正的方法而不是 mocking 它。
到目前為止,我已經嘗試了所有可用的幫助,但沒有運氣。
請指教我做錯了什么?
您正在修補錯誤的名稱。 ResolveCustomer.resolve_customer
使用schema.get_customer
,而不是authorization.get_customer
。 結果,沒有使用mock_get_customer
並且實際的 function 被調用(使用任何kwargs
綁定作為其參數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.