尝试测试拦截器时的角度测试随机崩溃

时间:2020-03-11 16:03:16

标签: angular jasmine karma-runner interceptor

我正在测试一个Angular拦截器,该拦截器会在每次api调用之前测试刷新令牌

当api响应出现错误时,客户端必须注销

大多数情况下都可以使用,但是有些时候我可以使用

Chrome 80.0.3987(Windows 10.0.0)错误 毕竟发生了错误 HttpErrorResponse:对https://localhost:44310/api/autoauth/refreshtoken的Http错误响应: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();
  });

0 个答案:

没有答案