我的任务是为我们的生产流水线创建单元测试代码,而不需要修改生产流水线。流水线具有写入和读取队列的方法。我正在使用mock覆盖这些调用,以创建单个“单元”测试。但是我被最后一部分卡住了。
我需要访问在管道中创建的对象,但是创建它的方法不返回该对象。将对象设置为self也是不可接受的,这样在方法返回后它就会留在内存中。
我们想知道是否有一种方法可以在类方法运行时注入它,这样我就可以在方法返回之前检索生产对象。
我在下面创建了一个虚拟示例,但方法是相同的。如果你有问题或者我解释得不够好,请告诉我。谢谢。
class production(object):
def __init__(self):
self.object_b = 'Test Object'
def run(self):
"Other lines of code"
object_a = self.create_production_object()
"Other lines of code"
"Test object here"
return 0
def create_production_object(self):
return 'Production Object'
test_prod_class = production()
test_prod_class.run()
assert(test_prod_class.object_a, 'Production Object')发布于 2018-02-07 00:44:44
如何重写创建对象的方法,使其也将对象存储在self中
class MockProduction(production):
def create_production_object(self):
self.object_a = super(MockProduction, self).create_production_object()
return self.object_ahttps://stackoverflow.com/questions/48647766
复制相似问题