dimanche 30 août 2020

angular test http call inside a service function

I am trying to test http call inside of the service method. I don't understand why my test doesn't init http call inside that funcion, even if I am using tick method.

logout() 
{
    this.http.post(environment.apiUrl + '/logout ', {}).subscribe(
      () => {
        localStorage.removeItem('token');
        localStorage.removeItem('user');
        this.router.navigate(['']);
      }, err => console.log(err)
    );
  }

and test for this method:

it('should be send to correct URL', fakeAsync(() => {
      service.logout();
      const req = httpTestingController.expectOne(`${environment.apiUrl}/logout`);
      req.flush({});
      tick();
      httpTestingController.verify();
    }));

Aucun commentaire:

Enregistrer un commentaire