如何编写单元测试以涵盖断点观察器的所有条件?

问题描述

我尝试编写单元测试断点观察器。但没有涵盖阳性条件

 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();
  });
}

enter image description here

帮我为该行编写单元测试

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%');
});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...