当 Observable 中的响应为 500(发生错误)时,是否有某种方法可以测试内部实现

Is there some way to test the inner implementation when response in Observable is 500 (Error occured)

下面是代码

@Output() hitCodeReturned = new EventEmitter<boolean>();

ngOnInit() {
  this.someService.method().subscribe(
  () => {
    // Some code here
  },
  ({ error }) => {
    if (condition equals true) {
      this.hitCodeReturned.emit(true);
    }
  }
);

}

测试应确保当响应 return 某些主体

出错时将发出 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();
        
       
    }