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

时间:2016-06-21 18:08:21

标签: angularjs angular jasmine ionic2

我试图在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

1 个答案:

答案 0 :(得分:1)

您可以使用fakeAsync和flushMicrotasks或tick来在角度/核心/测试中传递时间。 我的一个测试可能看起来像

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