我在测试我的项目时遇到了一些困难,主要是因为控制器引用了一个工厂生产的单件产品。你知道吗
这个问题的一个简单例子是:
你知道吗数据库工厂.py你知道吗
class DataBaseFactory(object):
# Lets imagine we support a number of databases. The client implementation all gives us a similar interfaces to use
# This is a singleton through the whole application
_database_client = None
@classmethod
def get_database_client(cls):
# type: () -> DataBaseClientInterFace
if not cls._database_client:
cls._database_client = DataBaseClient()
return cls._database_client
class DataBaseClientInterFace(object):
def get(self, key):
# type: (any) -> any
raise NotImplementedError()
def set(self, key, value):
# type: (any, any) -> any
raise NotImplementedError()
class DataBaseClient(DataBaseClientInterFace):
# Mock some real world database - The unittest mocking should be providing another client
_real_world_data = {}
def get(self, key):
return self._real_world_data[key]
def set(self, key, value):
self._real_world_data[key] = value
return value
你知道吗型号.py你知道吗
from .databasefactory import DataBaseFactory
class DataModel(object):
# The DataBase type never changes so its a constant
DATA_BASE_CLIENT = DataBaseFactory.get_database_client()
def __init__(self, model_name):
self.model_name = model_name
def save(self):
# type: () -> None
"""
Save the current model into the database
"""
key = self.get_model_key()
data = vars(self)
self.DATA_BASE_CLIENT.set(key, data)
@classmethod
def load(cls):
# type: () -> DataModel
"""
Load the model
"""
key = cls.get_model_key()
data = cls.DATA_BASE_CLIENT.get(key)
return cls(**data)
@staticmethod
def get_model_key():
return 'model_test'
你知道吗数据控制器.py你知道吗
from .databasefactory import DataBaseFactory
from .model import DataModel
class DataBaseController(object):
"""
Does some stuff with the databaase
"""
# Also needs the database client. This is the same instance as on DataModel
DATA_BASE_CLIENT = DataBaseFactory.get_database_client()
_special_key = 'not_model_key'
@staticmethod
def save_a_model():
a_model = DataModel('test')
a_model.save()
@staticmethod
def load_a_model():
a_model = DataModel.load()
return a_model
@classmethod
def get_some_special_key(cls):
return cls.DATA_BASE_CLIENT.get(cls._special_key)
@classmethod
def set_some_special_key(cls):
return cls.DATA_BASE_CLIENT.set(cls._special_key, 1)
最后是单元测试本身: 试验_简单.py你知道吗
import unittest
from .databasefactory import DataBaseClientInterFace
from .datacontroller import DataBaseController
from .model import DataModel
class MockedDataBaseClient(DataBaseClientInterFace):
_mocked_data = {DataBaseController._special_key: 2,
DataModel.get_model_key(): {'model_name': 'mocked_test'}}
def get(self, key):
return self._mocked_data[key]
def set(self, key, value):
self._mocked_data[key] = value
return value
class SimpleOne(unittest.TestCase):
def test_controller(self):
"""
I want to mock the singleton instance referenced in both DataBaseController and DataModel
As DataBaseController imports DataModel, both classes have the DATA_BASE_CLIENT attributed instantiated with the factory result
"""
# Initially it'll throw a keyerror
with self.assertRaises(KeyError):
DataBaseController.get_some_special_key()
# Its impossible to just change the DATA_BASE_CLIENT in the DataBaseController as DataModel still points towards the real implementation
# Should not be done as it won't change anything to data model
DataBaseController.DATA_BASE_CLIENT = MockedDataBaseClient()
self.assertEqual(DataBaseController.get_some_special_key(), 2)
# Will fail as the DataModel still uses the real implementation
# I'd like to mock DATA_BASE_CLIENT for both classes without explicitely giving inserting a new class
# The project I'm working on has a number of these constants that make it a real hassle to inject it a new one
# There has to be a better way to tackle this issue
model = DataBaseController.load_a_model()
unittest导入DataBaseController时,DataModel通过DataBaseController模块导入。 这意味着两个database和CLIENT类变量都被实例化。 如果我的工厂捕捉到它在unittest内部运行,那么导入发生在unittest之外,这仍然无关紧要。你知道吗
我的问题是:有没有一种方法可以模拟这个单例并立即替换整个应用程序?你知道吗
替换工厂上的缓存实例不是一个选项,因为类中的引用指向旧对象。你知道吗
将这些单例实例作为类变量放在首位可能是一个设计缺陷。但是我宁愿检索一个类变量,而不是每次为单例调用工厂。你知道吗
在您的用例中,单个模块负责向整个应用程序提供单例。因此,我将尝试在模块中注入mock,然后再由其他模块使用。问题是在声明其他类之前无法完全构造mock。一种可能的方法是在两个过程中构造单例:第一个过程不依赖于任何东西,然后使用最小对象来构造类,然后填充其内部术语表。代码可以是:
但是要注意:这是假设测试过程没有加载型号.py或者数据控制器.py试验前_简单.py你知道吗
相关问题 更多 >
编程相关推荐