Unit testing an observable in Angular 2

后端 未结 6 1940
灰色年华
灰色年华 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 10:55

    https://angular.io/guide/testing currently shows a few ways. Here is one:

    it('#getObservableValue should return value from observable',
        (done: DoneFn) => {
           service.getObservableValue().subscribe(value => {
           expect(value).toBe('observable value');
           done();
        });
    });
    

提交回复
热议问题