我正在输入一些服务列表的类型脚本类
我定义了一个属性,该属性应引用ServiceCatalog
该类,如下所示:
export default class myGenerator extends Generator {
private svcat: ServiceCatalog | undefined;
// and here I’ve initilzied the property
await this.getSvc();
// here I created some function the return service instances
private async getSvc() {
this.svcat = new ServiceCatalog();
await this.svcat.getServiceInstances();
}
// and here I’ve additional code which use it
this.svcat.values ….
我的问题是在javascript / typescript中有更好/更干净的方法吗?也许不使用this
关键字...
也许还有更好的测试代码(单元测试)...
您今天的工作方式很难测试。这是为什么?好吧,因为如果您想将自己的Generator
班级与自己的班级隔离开来ServiceCatalog
,您将很难。
我建议什么,像上面的同事,是有ServiceCatalog
由客户来但有一个默认值。
class MyGenerator extends Generator {
constructor(private svCat: ServiceCatalog = new ServiceCatalog()) {
super();
}
}
这样,您可以像
new MyGenerator()
或用于测试
new MyGenerator(myFakeServiceCatalog)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句