Unit testing an observable in Angular 2

后端 未结 6 1941
灰色年华
灰色年华 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:51

    I recommend this approach, which I think is more elegant:

    expectAsync(carService.getCars().toPromise()).toBeResolvedWith(myExpectedValue);
    

    You can also provide your own async matchers using: Jasmine Matcher

提交回复
热议问题