单元测试可观察到角度2+

时间:2020-04-24 09:39:35

标签: angular unit-testing user-interface karma-jasmine

如何编写返回可观察值的函数的单元测试用例

public notificationStream(): Observable<Notification> {
return this.notificationSubject.asObservable();

}

1 个答案:

答案 0 :(得分:0)

一种方法可能是这样

it('should check if notificationStream works correctly', () => {
   let mockResponse = { prepare your mock Response}
   this.component.notificationStream().subscribe(resoponse => {
     expect(response).toBe(mockResponse)
   })
   //call function from you component which contains following line 
   //this.notificationSubject.next(mockResponse);
  });

订阅后,您将获得传递给.next()的所有值作为响应