首页 > 解决方案 > 如何让 Angular 测试在执行之前等待先前的异步操作完成

问题描述

在我的 Angular 应用程序测试套件中,我有一个测试用例执行以下操作:

it('test case with async operation', async(() => {
  // open modal

  // do some stuff

  fixture.whenStable().then(() => {
    // do some async stuff and close the modal

    // some expetations
  });
}));

一切正常,除了当这个测试真正完成执行时,模态仍然打开(whenStable回调将稍后执行,关闭模态)。这使得其他测试失败,因为模态仍然会打开一段时间。

请注意,模态不是夹具组件的一部分,因此我使用它进行了查询document.querySelector

请注意,测试用例被包装到async.

问题是我必须等待模式内的异步操作,这迫使我使用asyncand whenStable。为此,我也尝试使用fakeAsyncandtick而不是async,但这会给我错误error 1 timer(s) still in the queue

标签: angulartypescriptngx-bootstrapangular-testngx-bootstrap-modal

解决方案


推荐阅读