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

Time:05-13

Here is the code below

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

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

}

The test should ensure that true will be emitted when response will return error with some body

CodePudding user response:

yes, just use throwError operator in test

 //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();
        
       
    }    
  • Related