Unit testing an observable in Angular 2

后端 未结 6 1918
灰色年华
灰色年华 2020-12-24 10:17

What is the correct way of unit testing a service returning an Observable result in Angular 2? Let\'s say we have a getCars method in a CarService service class:

<         


        
6条回答
  •  醉梦人生
    2020-12-24 11:07

    AsyncTestCompleter is deprecated https://github.com/angular/angular/issues/5443. injectAsync replaced it https://github.com/angular/angular/issues/4715#issuecomment-149288405
    but injectAsync is now also deprecated
    injectAsync is not deprecated anymore https://github.com/angular/angular/pull/5721 (see also comment from @ErdincGuzel)

    it('retrieves all the cars', injectAsync( [CarService], ( carService ) => {
         var c = PromiseWrapper.completer();
         carService.getCars().subscribe( result => {         
             expect(result.length).toBeGreaterThan(0);
             c.resolve();
         } ); 
         return c.promise;      
    }) );
    

提交回复
热议问题