下面的代码
@Output() hitCodeReturned = new EventEmitter<boolean>();
ngOnInit() {
this.someService.method().subscribe(
() => {
// Some code here
},
({ error }) => {
if (condition equals true) {
this.hitCodeReturned.emit(true);
}
}
);
}
测试应该确保当响应返回带有某些正文的错误时将触发true
是,在测试中只使用throwError
算子
//in jasmine/karma test
//assuming that someService is a `mock`
it('propagates hitCodeReturned', (done)=>{
someService.and.return(throwError("here you can throw any kind of error"));
component.hitCodeReturned().subscribe(code=>{
expect(code).toBeTrue();
done();
});
component.ngOnInit();
}