我正在尝试做以下事情:
def test_fn(self):
cool_dict = {}
blah = Mock(spec=DictField, wraps=cool_dict)
blah['key'] = 'val'
print(cool_dict))
return False
基本上,我想确保对blah
发生的任何事情都允许在一个DictField
中发生,但是我希望对blah
发生的任何事情实际上都会发生在cool_dict
中,这样我就可以断言它具有某种状态。
我该怎么做?上面的代码失败了:
FAILED (errors=1)
Error
Traceback (most recent call last):
File "C:\Program Files\Python 3.5\Lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\Program Files\Python 3.5\Lib\unittest\case.py", line 605, in run
testMethod()
File "C:\Users\danie01.AD\PycharmProjects\component\component\Default\Functional\pam_team_management\test\test_team_create.py", line 23, in test_populate_info_page
blah['key'] = 'val'
TypeError: 'Mock' object does not support item assignment
我也使用 MagicMock
进行了尝试:
def test_populate_info_page(self):
cool_dict = {}
blah = MagicMock(spec=DictField, wraps=cool_dict)
blah['key'] = 'val'
print(cool_dict)
return False
虽然没有失败,但cool_dict仍然是{}