我正在尝试在模拟函数_read_device_beans
上注意到函数BeanMock._read_device_beans
,但看到了原始函数的调用
@patch('configurator.DeviceConfigurator')
class DeviceConfigurator:
def __init__(self, bean_map):
self._bean_map = bean_map
self._beans = None
新课程:
class BeanMock:
def __init__(self, index):
self._beans = None
self.index = index
模拟功能:
@classmethod
def _read_device_beans(self, parameters: L3InterfaceParameters):
...
测试类:
class TestManager(unittest.TestCase):
def setUp(self):
self.module_mock = MagicMock()
测试功能:
def test_l3if_read(self):
connector = DeviceConnection()
l3_interface_configurator = L3InterfaceConfigurator(connector)
param = L3InterfaceParameters()
with patch.dict('sys.modules', **{
'configurator': self.module_mock,
'configurator.l3_interface': self.module_mock,
'configurator.DeviceConfigurator._read_device_beans':
BeanMock._read_device_beans(param),
});
...
try:
res_param = l3_interface_configurator._read(param)
print(res_param)
self.assertEqual(res_param, "incorrect index", "incorrect return wrong parameters")
except Exception as e:
print(e)
assert True
class L3InterfaceConfigurator(Configurator):
def __init__(self, connection):
super(L3InterfaceConfigurator, self).__init__(bean_map, connection)
def _read(self, parameters: L3InterfaceParameters) -> L3InterfaceParameters:
self._read_device_beans(parameters)
if self._beans:
return parameters
class DeviceConfigurator(Configurator):
__metaclass__ = ABCMeta
READ = 'read'
UPDATE = 'update'
DEPLOY = 'deploy'
DELETE = 'delete'
READ_ALL = 'read_all'
def __init__(self, bean_map):
self._bean_map = bean_map
self._beans = None
log.info(' {0} initialized, server: {1}'.format(self.__class__.__name__, self.id))
def _read_device_beans(self, parameters):
...
self._struct_load(parameters)