mercredi 29 novembre 2017

Unit Test return observables

Is the method I'm testing. The thing I'm trying to test is if the return next.handle(req); is returning a correct value. I'm not quiet sure how to do this.

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        console.log(req);
        if(req['url'].indexOf('/token') > -1 ){
            return next.handle(req);
        }
        const authHeader = this.tokenService.getToken();    
        if(authHeader){
            const authReq = req.clone({headers: req.headers.set('Authorization', 'Bearer ' + authHeader)});
            return next.handle(authReq);
        }else{
            this.router.navigate([Config.authenticatePage]);
            return next.handle(req);
        }
    }

Test

it(('(intercept) should get token '), () => {
        sut.intercept(req, next);
        req['url'] = '/token';
        expect(sut.intercept(req, next)).toEqual(next.stub(req));               
    });

Aucun commentaire:

Enregistrer un commentaire