1

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

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

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

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

我应该注意,在测试MatStepperModule中是在进口中。

4

1 回答 1

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);
});
于 2021-07-20T11:20:39.853 回答