簡體   English   中英

如何編寫單元測試來覆蓋斷點觀察器的所有條件?

[英]how to write unit test to cover all the condition of breakpoint observer?

我嘗試編寫單元測試斷點觀察器。 但陽性條件不包括在內

 isSmallScreen: Observable<BreakpointState> = this.breakpointObserver.observe('(max-width: 767px)');

 openEntityDetailDialog(): void {   
        this.entityCreationComponent.close();
      this.dialogRef = this.dialog.open(HomeBusinessEntityDetail, {
        maxWidth: '767px', disableClose: true
      });
   const dialogSubscription = this.isSmallScreen.subscribe(result => {
    if (result.matches) {
      this.dialogRef.updateSize('100%', '100%');
    } else {
      this.dialogRef.updateSize('560px');
    }
  });
 
  this.dialogRef.afterClosed().subscribe(result => {
    dialogSubscription.unsubscribe();
  });
}

在此處輸入圖片說明

幫我寫行的單元測試

const dialogSubscription = this.isSmallScreen.subscribe(result => {
    if (result.matches) {
      this.dialogRef.updateSize('100%', '100%');
    } 

提前致謝。

你可以試試這樣的

it('should update the dialog size', () => {
     component.isSmallScreen = of({ matches: true });
     const spy: Jasmine.Spy = spyOn(component.dialogRef, 'updateSize'); // considering you have mocked the dialogRef in your test
     component.openEntityDetailDialog();
     expect(spy).toHaveBeenCalledWith('100%', '100%');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM