Angular async/await和async/fixture.whenStable在角度上的差异

Angular async/await和async/fixture.whenStable在角度上的差异,angular,unit-testing,asynchronous,async-await,karma-jasmine,Angular,Unit Testing,Asynchronous,Async Await,Karma Jasmine,我想知道在Angular框架中测试时处理异步调用的这两种方法之间的区别: 第一种是jasmine方法异步/等待 第二种是角度接近异步/固定装置。当稳定时 它们相似吗?如果没有,区别是什么,我应该在什么时候使用其中一种而不是另一种?异步/await的第一种方法是库存JavaScript,您希望异步运行函数,并且可以在继续下一行之前等待承诺 it('it block in an async await way', async(done) => { await waitForThisF

我想知道在Angular框架中测试时处理异步调用的这两种方法之间的区别:

  • 第一种是jasmine方法异步/等待
  • 第二种是角度接近异步/固定装置。当稳定时

它们相似吗?如果没有,区别是什么,我应该在什么时候使用其中一种而不是另一种?

异步/await的第一种方法是库存JavaScript,您希望异步运行函数,并且可以在继续下一行之前等待承诺

it('it block in an async await way', async(done) => {
   await waitForThisFunctionThatReturnsAPromiseBeforeCarringForward();
   // do something, make assertions
   const x = await getXFromAPromise(); // wait for getXFromAPromise() function to return the promise
// and assign the results to x
   // do something, make assertions
   done(); // call done to ensure you have run through the whole it block and tell Jasmine you're done
});
fixture.whenStable基本上是等待堆栈中的所有承诺在继续断言之前得到解决

it('demonstration of fixture.whenStable', async(done) => {
   // do some actions that will fire off promises
   await fixture.whenStable(); // wait until all promises in the call stack have been resolved
   // do some more assertions
   done(); // call done to tell Jasmine you're done with this test.
});
done回调是可选的,但我使用它来确保更好的工程(确保它遍历整个it块)

编辑====================

为了处理可观察的对象,我使用了两种方法

async/await
take
toPromise
操作员一起执行第一次发射并将其转换为承诺。在
执行(1)
之前,您可以随意添加其他操作员,如
过滤器
,以忽略一些排放

另一种方法是使用
done
回调
subscribe

it('should do xyz', done => {
  component.observable$.subscribe(result => {
    expect(result).toBe(...);
    // call done here to ensure the test made it within the subscribe
    // and did the assertions and to let Jasmine know you're done with the tests
    done();
  });
});

谢谢你回答我!我想知道这两种方法是可观测的还是仅用于承诺?这些方法仅用于承诺。我编辑了我的答案,向大家展示了我是如何处理可见光的。谢谢你的详细解释!它帮助了我很多,所以我应该在测试中使用vanilla async/wait还是waitForAsync?这真的有关系吗?@Red2678没关系,这是偏好的问题。
it('should do xyz', done => {
  component.observable$.subscribe(result => {
    expect(result).toBe(...);
    // call done here to ensure the test made it within the subscribe
    // and did the assertions and to let Jasmine know you're done with the tests
    done();
  });
});