【问题标题】:Unit test-Give a fake value to the variables initialized by constructor单元测试——给构造函数初始化的变量一个假值
【发布时间】:2017-09-14 01:35:21
【问题描述】:

我有课

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 我将从实例化中得到它。我怎样才能做到这一点?

【问题讨论】:

  • 提示:使用“StackSnippet”实用程序并单击<> 按钮来格式化您的代码。
  • 请问您可以编辑您的问题吗?否则,在当前状态下,它可能会被关闭。
  • 这是从哪里来的?我给了一个提示,并要求你格式化你的代码,仅此而已。
  • “请专注于主要目标”我认为你需要重新阅读How to Ask

标签: unit-testing typescript mocha.js sinon chai


【解决方案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?

【讨论】:

  • 感谢您的回复。我不能向上移动属性,因为我只想在实例化时初始化它。所以我必须在那里给出一个假值
猜你喜欢
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 2015-02-19
  • 2015-04-28
  • 1970-01-01
相关资源
最近更新 更多