角度检测发生变化问题

时间:2018-02-15 14:57:14

标签: angularjs unit-testing jasmine

我遇到了fixture.detectChanges();我会在每个人之前拥有这个。

  beforeEach(() => {
    fixture = TestBed.createComponent(Testcomponent);
    component = fixture.componentInstance;
    component.numArr = [1]; // numArr is an @Input 
     fixture.detectChanges();
  });

然后在测试用例中,我会有一些推送到该数组的东西。

it('update correctly', () => {
    component.numArr.push(2);
    fixture.detectChanges();
    let numbers = fixture.debugElement.queryAll(By.css('nums'))
    expect(numbers.length).toEqual(component.numArr.length);
})

css查询将返回1,而numArr的长度为2.它似乎仅在第一个fixture.detectChanges()之后更新。任何后续调用都不会更新。

编辑:我看到了http://www.bentedder.com/fixture-detectChanges-not-working-angular-4-karma-tests/。并且它似乎不是一个好的答案,因为我试图测试var何时更新。

Edit2:HTML

<div>
 <select >
  <option class="nums" *ngFor="let num of numArr" [ngValue]="num" >
    {{num}}
  </option>
</select>
</div>

0 个答案:

没有答案
相关问题