[英]How to write unit test on a angular component that use material stepper
Angular app use material stepper that is defined in template. Angular 应用程序使用模板中定义的材料步进器。 The component access the stepper by using the @ViewChild('stepper') stepper: MatStepper;
组件通过使用@ViewChild('stepper') 步进器访问步进器: MatStepper; In the component, many method set the selected index based on click event.
在组件中,许多方法根据点击事件设置选定的索引。 Unit test fail since it say "Cannot set properties of undefined (setting 'selectedIndex')".
单元测试失败,因为它说“无法设置未定义的属性(设置'selectedIndex')”。
I think stepper is defined in template(ie html), the unit test is on component(.ts) and used viewchild to access it so on unit test could not access stepper since it is not defined in component.我认为步进器是在模板(即 html)中定义的,单元测试在组件(.ts)上并使用 viewchild 来访问它,因此单元测试无法访问步进器,因为它没有在组件中定义。
How do I write unit test component that has material stepper.如何编写具有材料步进器的单元测试组件。
You need to make sure that you have configured the TestBed correctly, and imported all relevant dependencies.您需要确保已正确配置 TestBed,并导入所有相关依赖项。 Without seeing your code, try to import the MatStepperModule using:
在没有看到您的代码的情况下,尝试使用以下命令导入 MatStepperModule:
import {MatStepperModule} from '@angular/material/stepper';
and then add it to you TestBed:然后将其添加到您的 TestBed:
TestBed.configureTestingModule({
imports: [
..., MatStepperModule
]
});
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.