[英]Unit test for observable in angular 2+
How to write the unit test case of the function which returns an observable如何编写返回 observable 的 function 的单元测试用例
public notificationStream(): Observable<Notification> {
return this.notificationSubject.asObservable();
} }
One approach can be like this一种方法可以是这样的
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);
});
After subscription you will get all values in response that you pass to .next()
订阅后,您将获得传递给.next()
的所有响应值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.