单元测试 - 为构造函数初始化的变量赋予伪值

时间:2017-04-18 09:24:00

标签: unit-testing typescript mocha sinon chai

我有一个班级

Test.ts:

class Test
{
     public property1: string;
     public property2: boolean;
     constructor(property1, property2)
     {
            this.property1 = property1;//will get value while instantiation
            this.property2 = property2;//will get value while instantiation
            this.property3 = somevalue;//want to add fake value here 
     }
}

如上所述,我想为property3提供假值但property1property2我将从实例化中获取它。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:-1)

我相信你正在努力宣布property3成员。就像你宣布其他属性一样。如果您需要,可以使用属性初始值设定项:

class Test
{
     public property1: string;
     public property2: boolean;
     public property3 = somevalue;
     constructor(property1, property2)
     {
            this.property1 = property1;//will get value while instantiation
            this.property2 = property2;//will get value while instantiation
     }
}

更多

有关TypeScript类https://basarat.gitbooks.io/typescript/docs/classes.html

的一些文档