2016-06-21 5 views
0

私は、Angular2で約束をテストするには、このような何かしようとしている:

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

This articleは私がこれを行うことができます知っているアンギュラ1. のための約束をテストする方法を示しています。

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

をしかし、私はこのエラーが発生し続けるので、$provide.valueが私が使用できるいくつかの他の機能であることを望んでいます。'undefined' is not an object (evaluating 'this.fooService.getData().then') thenが好きではありません。

答えて

1

角度/コア/テストで時間を渡すには、fakeAsyncとflushMicrotasksまたはtickを使用できます。 私のテストの1つは、何かのように見えるかもしれません

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

あなたの助けをありがとう、私はこれを試してみます – jhhoff02

関連する問題