Jasmine:测试承诺。 Angular2 有类似 $provide.value() 的东西吗?

Jasmine: Testing promises. Is there something like $provide.value() for Angular2?

我正在尝试测试 Angular2 中的承诺,如下所示:

this.fooService.getData().then(data => this.fooData = data);

This article 展示了如何测试 Angular 1 的承诺。 我知道我可以做到:

beforeEachProviders(() => [
  provide(FooService, {useClass: fooService})
]);

但我希望 $provide.value 是我可以使用的其他一些功能,因为我一直收到此错误 'undefined' is not an object (evaluating 'this.fooService.getData().then') 它不喜欢 then.

你可以使用fakeAsync和flushMicrotasks或者tick来打发angular/core/testing中的时间。 我的一项测试可能看起来像

it('should set the active module', <any>fakeAsync(() => {
   let expected = { id: 'id1' };
   jasmine.createSpyObj<FooService>('FooService'['getData']);
   spyService.fooService.and.callFake(() => {
      return Promise.resolve(expected);
   });
   let component = new FooComponent(fooService);
   component.getData();
   flushMicrotasks();
   expect(component.fooData).toEqual(expected);
}));