简体   繁体   English

Angular 中 async/await 和 async/fixture.whenStable 的区别

[英]Difference between async/await and async/fixture.whenStable in Angular

I want to know the difference between these two methods for dealing with asynchronous calls in the Angular framework while testing:测试时想知道Angular框架中这两种处理异步调用的方法的区别:

  • The first with the jasmine approach async/await第一个使用 jasmine 方法异步/等待
  • The second with the Angular approach async/fixture.whenStable第二个用 Angular 方法async/ fixture.whenStable

Are they similar?它们相似吗? If not, what is the difference, and when exactly should I use one rather than the other?如果不是,有什么区别,我应该什么时候使用一个而不是另一个?

The first approach of async/await is stock JavaScript where you want to run the function asynchronously and you can await for promises before carrying over to the next line. async/await的第一种方法是库存 JavaScript,您希望在其中异步运行 function,您可以等待承诺,然后再转到下一行。

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
});

The fixture.whenStable basically waits for all promises in the stack to be resolved before carrying forward with assertions. fixture.whenStable基本上会等待堆栈中的所有 Promise 都被解决,然后再继续进行断言。

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.
});

The done callback is optional but I use it to ensure for better engineering (make sure it traversed through the whole it block). done 回调是可选的,但我使用它来确保更好的工程(确保它遍历整个 it 块)。

Edit ====================编辑 =====================

To handle observables, I use two methods.为了处理 observables,我使用了两种方法。

async/await with take and toPromise operator where you take the first emission and you convert it to a promise. async/await使用taketoPromise操作符,您在其中获取第一个发射并将其转换为 promise。 Feel free to add other operators such as filter to ignore some emissions before the take(1) .随意添加其他运算符,例如filtertake(1)之前忽略一些排放。

import { take } from 'rxjs/operators';
......
it('should do xyz', async done => {
  const x = await component.observable$.pipe(take(1)).toPromise();
  expect(x).toBe(....);
  done();
});

The other way is to subscribe with done callback另一种方法是subscribe done回调

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();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM