jeudi 7 décembre 2017

test Observable.throw Angular

I'm trying to test the .catch part in this code:

send(cart: Cart): Observable<any> {
return this.tpvService.sendToTPV(cart).catch((res: any) => {
    return Observable.throw(res.error.products);
  });
}

TpvService.sendToTPV only call to the http return this.http.post('endpoint')

The test is:

it('should catch the error', async(() => {
    spyOn(tpvRemote, 'sendToTPV').and.returnValue(Observable.throw({}));
       tpvService.sendToTPV(mocks.cart).subscribe(() => {
    });
}));

The output is:

PhantomJS 2.1.1 (Mac OS X 0.0.0) TPV Service send cart to tpv should catch the error FAILED Failed http://localhost:9876/_karma_webpack_/vendor.bundle.js:111553:30 http://localhost:9876/_karma_webpack_/vendor.bundle.js:111604:29 invoke@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:18139:31 onInvoke@http://localhost:9876/_karma_webpack_/vendor.bundle.js:59990:45 onInvoke@http://localhost:9876/_karma_webpack_/vendor.bundle.js:60808:47 invoke@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:18138:40 run@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:17889:49 http://localhost:9876/_karma_webpack_/vendor.bundle.js:111599:28 onHandleError@http://localhost:9876/_karma_webpack_/vendor.bundle.js:60000:31 onHandleError@http://localhost:9876/_karma_webpack_/vendor.bundle.js:60816:52 handleError@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:18143:50 runGuarded@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:17905:55 runInTestZone@http://localhost:9876/_karma_webpack_/vendor.bundle.js:111609:35 http://localhost:9876/_karma_webpack_/vendor.bundle.js:111548:26 invoke@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:18139:31 onInvoke@http://localhost:9876/_karma_webpack_/vendor.bundle.js:60811:45 invoke@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:18138:40 run@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:17889:49 http://localhost:9876/_karma_webpack_/vendor.bundle.js:60506:37 execute@http://localhost:9876/_karma_webpack_/vendor.bundle.js:60536:46 invokeTask@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:18172:36 runTask@http:// localhost:9876/_karma_webpack_/polyfills.bundle.js:17939:57 drainMicroTaskQueue@http:// localhost:9876/_karma_webpack_/polyfills.bundle.js:18349:42 run@http:// localhost:9876/_karma_webpack_/polyfills.bundle.js:6181:29 http:// localhost:9876/_karma_webpack_/polyfills.bundle.js:6194:33 flush@http:// localhost:9876/_ karma_webpack_/polyfills.bundle.js:2440:11

I don't understand why it is showing only Failed in the console. The rest of tests are working fine.

I think that the problem is return Observable.throw(res.error.products); because if I change this line for return Observable.of(res.error.products); everything is working.

Aucun commentaire:

Enregistrer un commentaire