def create_section(config, section):
config.reload()
if section not in config:
config[session] = {}
logging.info("Created new section %s.", section)
else:
logging.debug("Section %s already exists.", section)
我想写一些单元测试,但我遇到了一个问题。例如,
def test_create_section_created():
config = Mock(spec=ConfigObj) # ← This is not right…
create_section(config, 'ook')
assert 'ook' in config
config.reload.assert_called_once_with()
显然,测试方法将失败,因为TypeError
'Mock' 类型的参数不可迭代。
如何将config
对象定义为模拟?