Angular Test Random Cra sh, когда я пытаюсь проверить перехватчик - PullRequest
0 голосов
/ 11 марта 2020

Я тестирую перехватчик Angular, который проверяет токен refre sh перед каждым вызовом API

Когда в ответ API с ошибкой клиент должен выйти из системы

Большинство из время это работает, но иногда я получаю

Chrome 80.0.3987 (Windows 10.0.0) ERROR Ошибка afterAll HttpErrorResponse: Http ошибка отклика для https://localhost: 44310 / api / autoauth / refreshtoken : 401 Несанкционировано

и после

Chrome 80.0.3987 (Windows 10.0.0) ОШИБКА отключена, поскольку в течение 30000 мсек нет сообщений.

Здесь

class SubjectAuthService extends AuthenticationService {
  logout() { }
}

describe(`JwtRefreshInterceptor`, () => {
  let service: FamilyService;
  let httpMock: HttpTestingController;
  let authenticationService: AuthenticationService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule,
        GtagTestingModule,
      ],
      providers: [
        FamilyService,
        { provide: AuthenticationService, useClass: SubjectAuthService },
        { provide: 'familyTableName', useValue: 'families' },
        {
          provide: HTTP_INTERCEPTORS,
          useClass: JwtRefreshInterceptor,
          multi: true,
        },
      ],
    });


    authenticationService = TestBed.get(AuthenticationService);
    service = TestBed.get(FamilyService);
    httpMock = TestBed.get(HttpTestingController);

  });


  it('should logOut because no current user', () => {
    spyOn(authenticationService, 'logout');

    service.fetchFamilies().subscribe();
    httpMock.expectNone(`${environment.api}/family?catalogId=0`);

    const refreshRequest = httpMock.expectOne(`${environment.api}/autoauth/refreshtoken`);
    expect(refreshRequest.request.method).toBe('POST');

    refreshRequest.flush(null, {
      status: 401,
      statusText: 'Unauthorized',
    });

    expect(authenticationService.logout).toHaveBeenCalled();
  });
...