Mat Stepper 作为视图子单元测试

问题描述

如何在这种情况下对 Material stepper 进行单元测试:

export class FooComponent implements OnInit {
    
    @ViewChild('stepper') stepper: MatStepper;
}
    bar() {
       this.stepper.selectedindex = 1;
    }
}

这会起作用,但是当使用 Jasmine 测试组件时,我会得到:

TypeError: 无法设置未定义的属性 'selectedindex'

我应该注意到在测试中 MatStepperModule 是导入的。

解决方法

我在 Jest 中编写了我的测试,但我相信你可以在 Jasmine 中做一些非常相似的事情,如果不一样的话。

it('bar should set stepper selectedIndex to 1',() => {
   
    component.stepper = { selectedIndex: 0 } as MatStepper;
    component.bar();
    fixture.detectChanges();

    expect(component.stepper.selectedIndex).toEqual(1);
});

相关问答

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