[英]Python, can't assert if method called from another class
我在 Python 中有以下代碼:
class A():
def doSomething(self, bClass):
print(bClass.theThing)
class B():
def __init__(self, theThing):
self.theThing = theThing
def foo():
a = A()
b = B("that thing")
a.doSomething(b)
我有這些類和 function foo() 存儲在 testing.py 中,我想測試 A 的方法是否被調用:
import testing, unittest
from unittest.mock import patch
class TheTestClass(unittest.TestCase):
def test(self):
with patch('testing.A.doSomething') as do:
testing.foo()
do.assert_any_call()
但我總是得到'doSomething() call not found'。 如果我能理解原因,我會更高興,但在這一點上,任何事情都是受歡迎的
幾個小時后,我開始弄清楚這一點。 就像 jwjhdev 所說assert_called_with()
需要一些東西,在我的情況下是 class 但assert_any_call()
也是如此。 出於某種原因,我認為assert_any_call()
會起作用,但我在想的是assert_called()
,它在沒有 arguments 的情況下也能正常工作。 最后,我通過將return b
添加到foo()
function 和:
def foo():
a = A()
b = B("that thing")
a.doSomething(b)
return b
class TheTestClass(unittest.TestCase):
def test(self):
with patch('testing.A.doSomething') as do:
b = testing.foo()
do.assert_any_call(b)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.