Executing Tests Asynchronously

Since services operate in an asynchronous manner it may be useful to execute a service's entire unit test asynchronously. This can speed up the overall time it takes to complete a full testing cycle since a particular long unit test will not block other unit tests from executing. We can set up our unit test to return a promise, which will resolve as either a success or failure depending on the activity of the test.

  1. describe('verify search', () => {
  2. it('searches for the correct term',
  3. fakeAsync(inject([SearchWiki, MockBackend], (searchWiki, mockBackend) => {
  4. return new Promise((pass, fail) => {
  5. ...
  6. });
  7. })));
  8. });

Instead of only using inject, we use fakeAsync to wrap it and fulfill dependencies and execute the test in an asynchronous process. Using fakeAsync requires us to return a Promise, which we use to resolve the competition of our test by calling pass, or fail, depending on the results of our test.

原文: https://angular-2-training-book.rangle.io/handout/testing/services/async-execution.html