如何编写返回可观察函数的单元测试用例
public notificationStream(): Observable<Notification> {
return this.notificationSubject.asObservable();
}
一种方法可能是这样
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()
的所有值作为响应>