我有以下結構:
# create.py
import sshHandler
class Create:
def __init__(self):
self.value = sshHandler.some_method()
# sshHandler.py
def some_method():
return True
如果我知道嘗試修補 sshHandler.some_method 它將無法按預期作業
from unittest import TestCase
from unittest.mock import patch
import create
class TestCreate(TestCase):
@patch("sshHandler.some_method")
def test_create(self, mock_ssh):
mock_ssh.return_value = False
c = create.Create()
# c.value = True but should be false
我正在尋找的結果是也some_method將在 create 中進行修補(并回傳 false)。如果我只是some_method在它的背景關系中呼叫test_create它會按預期作業。如何修復補丁,使其在訪問 sshHandler 時在 Create 類中也處于活動狀態?
我看到了這個問題Why python mock patch doesn't work?,但無法用那里提供的資訊解決我的問題。
uj5u.com熱心網友回復:
您修補了錯誤的模塊。而是打sshHandler.some_method補丁create.sshHandler.some_method。您必須修補您正在處理的模塊物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457277.html
上一篇:無法從“node_modules/@testing-library/react/dist/pure.js”中找到模塊“react-dom/client”
