Mat Stepper 作为视图子单元测试

时间:2021-03-05 00:43:23

标签: angular angular-material jasmine mat-stepper

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

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

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

<块引用>

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

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

1 个答案:

答案 0 :(得分:0)

我在 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);
});