Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 同步使用角度夹具.whenStable_Angular - Fatal编程技术网

Angular 同步使用角度夹具.whenStable

Angular 同步使用角度夹具.whenStable,angular,Angular,我对表单进行了同步测试: it('should do things' () => { const compiled = fixture.debugElement.nativeElement; button = compiled.querySelector('#buttonid'); button.click(); fixture.detectChanges(); fixture.whenStable().then(() => { expect(stat

我对表单进行了同步测试:

it('should do things' () => {
  const compiled = fixture.debugElement.nativeElement;

  button = compiled.querySelector('#buttonid');
  button.click();

  fixture.detectChanges();
  fixture.whenStable().then(() => {
    expect(state).toBe(whatIwant);
  }

  button.click();

  fixture.detectChanges();
  fixture.whenStable().then(() => {
    expect(state).toBe(whatIwant);
  }

});
这很有效,但我不明白为什么

我在网上读到的所有内容都表明,
whenStable
应该只在
async
测试中使用,但是上面的测试没有用async()包装器包装测试函数。如果我这样做,测试将失败(也就是说,预期将失败,因为这些更改似乎尚未反映在组件中)

根据这里的另一个答案,在同步测试中使用
whenStable
方法是毫无意义的:

否如果在没有异步的情况下进行测试,whenStable()将不会执行任何操作 伪异步。whenStable()的作用是等待测试中的所有任务 要完成的区域。当您不使用async进行测试时,NgZone不会 创建时,whenStable()会立即返回

但是,如果我相信他们的话,我省略了
whenStable
包装器,在调用fixture.detectChanges()之后进行断言,测试就会失败

因此,总结一下:

  • 当我同时使用
    async
    whenStable
    时,测试失败
  • 当我使用no
    async
    whenStable
    时,测试失败
  • 当我使用no
    async
    whenStable
    时,测试通过了:当我每次检查组件的状态时,状态已经更新到我期望的状态
此测试仅在我使用
时通过。我想知道的是:为什么?

因此,似乎与上述问题的公认答案相矛盾,whenStable在这里做了些什么。那到底是什么东西?我在这里省略的上下文(单击按钮等会发生什么)重要吗

根据角度文件:

whenStable()返回一个承诺,该承诺在JavaScript引擎的任务队列变为空时解析

我不确定这到底意味着什么:有人能澄清一下吗

此测试仅在我使用
whenStable
时通过。我想知道的是: 为什么?

这很可能是因为测试在
whenStable
中的异步操作执行(并失败)之前执行并完成,因此您最终得到的测试没有任何预期(因此不可能失败)