簡體   English   中英

Mocking a function called inside another function inside a class python

[英]Mocking a function called inside another function inside a class python

我需要在 function 藍圖中,在 class A 中模擬一個函數(this_function()),該函數在另一個函數(receive())中調用。我該怎么做? 我在我有所有測試的文件中為 this_function() 創建了一個模擬。

class A: 
       def blueprint(): 
           @route("/", method = ["POST"])
           async def receive(): 
                  ....
              test = await this_function()

我在與我的測試相同的文件中創建了一個模擬 function :

def mock_this_function(): 
   return ..

class Tests(unittest.TestCase): 
   @patch("path.to.classA.this_function")
    def test(mock_this_function):

this_function 在 helper.py 模塊中定義。

我無法連接/使用 mock_this_function。 它總是將我與 this_function 聯系起來......有什么解決方案嗎? 任何提示如何正確模擬它?

在你的模擬中嘗試這條路徑: path.to.classA.this_function ,你可能有錯誤的路徑。 我的意思是,你的 function 的嵌套程度並不重要。 如果是導入的,則只需正確引用模擬路徑即可,@HallerPatrick 提供的鏈接中對此進行了很好的描述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM