0
考慮以下幾點:使用Python unittest,我如何確保一個類被實例化?
class toTest(object)
def createObject(self):
self.created = toCreate()
class toCreate(object):
def __init__(self):
pass
# testFile.py
import unittest
class TestThing(unittest.TestCase):
def testCreated(self):
creator = toTest()
toTest.createObject()
# -- Assert that the 'toCreate' object was in fact instantiated
...我怎樣才能確保toCreate
實際上產生的?我試過以下內容:
def testCreated(self):
created = MagicMock(spec=toCreate)
toTest.createObject()
created.__init__.assert_called_once_with()
但是我得到以下錯誤:AttributeError: Mock object has no attribute 'init'
。我是否濫用了MagicMock類,如果是這樣的話?