简体   繁体   English

异步等待嵌套在 Jasmine 测试中

[英]Async await nested in Jasmine test

I'm new to Jasmine, I'm trying to do a unit test of the connection, the original function checks the browser on incognito, the test has to refer to that and do its respective check.我是 Jasmine 的新手,我正在尝试对连接进行单元测试,原来的 function 在隐身模式下检查浏览器,测试必须参考它并进行相应的检查。

then I leave the code of the component:然后我留下组件的代码:

ngOnInit(): void {
    this.getDetectPrivateMode();
    const auth = this.authServices.getAuth();
    if (auth) {
      this.loginSuccess();
    }
  }

  /**
   * Método para verificar ventanas modo incognito
   */
  async getDetectPrivateMode() {
    if (await this.helpers.detectPrivateMode()) {
      this.messageService.add({severity: 'error', summary: 'Error', detail: 'Cerrar modo incognito.', life: 2000});
      this.loginForm.disable();
      setTimeout(() =>{
        this.router.navigateByUrl('/error');
    }, 2000);
    }
  }

Now this is the way in which I am trying to do its respective verification:现在这是我尝试进行相应验证的方式:

describe('LoginComponen', () => {
  let httpTestingController: HttpTestingController;
  let helpers: Helpers;

beforeEach( async () => {

    await TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule.withRoutes(routes),
        ReactiveFormsModule,
        FormsModule,
        ConfirmDialogModule,
        BrowserAnimationsModule
      ],
      providers: [
        Helpers
      ],
      declarations: [
        LoginComponent
      ],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    }).compileComponents();
    
    // Peticiones mock
    helpers = TestBed.inject(Helpers)
    httpTestingController = TestBed.inject(HttpTestingController);
  });

it('Validar login incognito, (error)', (done: DoneFn) => {
    const fixture = TestBed.createComponent(LoginComponent);
    const app = fixture.componentInstance;
    fixture.detectChanges();

    const test = 1

    app.getDetectPrivateMode().then(async () => {
      if (await helpers.detectPrivateMode().then(()=>{return true})) {
        expect(test).toEqual(1)
        done();
      }
    })

  });
}

the result that gives me is the following给我的结果如下

enter image description here在此处输入图像描述

in the coverage result, the code does not pass through those lines在覆盖结果中,代码不会通过这些行

I would do the following (follow lines with:!):我会执行以下操作(遵循:!):

describe('LoginComponen', () => {
  let httpTestingController: HttpTestingController;
  let helpers: Helpers;
  // !! declare router
  let router: Router;

beforeEach( async () => {

    await TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule.withRoutes(routes),
        ReactiveFormsModule,
        FormsModule,
        ConfirmDialogModule,
        BrowserAnimationsModule
      ],
      providers: [
        Helpers
      ],
      declarations: [
        LoginComponent
      ],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    }).compileComponents();
    
    // Peticiones mock
    helpers = TestBed.inject(Helpers)
    httpTestingController = TestBed.inject(HttpTestingController);
    // Get a handle on the router
    router = TestBed.inject(Router);
  });

// !! Wrap in a fakeAsync so we can move time in a fake way with fakeAsync/tick
it('Validar login incognito, (error)', fakeAsync(() => {
    const fixture = TestBed.createComponent(LoginComponent);
    const app = fixture.componentInstance;
    fixture.detectChanges();

    // !! Spy on navigateByUrl
    const navigateByUrlSpy = spyOn(router, 'navigateByUrl');
    // !! make the helper return true
    spyOn(helper, 'detectPrivateMode').and.resolveTo(true);
   
    // !! Call method
    app.getDetectPrivateMode();
   
    // !! Make 2 seconds pass by
    tick(2000);
   
    // !! expect call to navigate to error page
    expect(navigateByUrlSpy).toHaveBeenCalledWith('/error');

  }));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM