【问题标题】:Count the number of child components by tag name in angular2 unit testing在angular2单元测试中按标签名称计算子组件的数量
【发布时间】:2017-08-29 12:16:25
【问题描述】:

我有一个父组件,它将根据数组的长度动态插入子组件。我想通过使用它的标签名称来测试创建的组件的数量。

<div *ngFor = "let item of phoneList; let i = index">
    <phone-item [item]="item"> </phone-item>
</div>

我想在 angular2 单元测试中验证在 html 模板中创建的&lt;phone-item&gt;&lt;/phone-item&gt; 的数量。

我对 debugElement 和 nativeElement 知之甚少。这就是我试图做的。

let el = fixture.debugElement.query(By.css('phone-item'));

但我想要创建子组件的数量。

【问题讨论】:

    标签: javascript unit-testing angular jasmine


    【解决方案1】:

    使用返回数组的DebugElement#queryAllquery 只返回第一个匹配项

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多