简体   繁体   English

如何在使用材料步进器的角度组件上编写单元测试

[英]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.

相关问题 如何在 Angular 5 中使用材料步进器 - How to use a material stepper in Angular 5 如何为 Angular 材料芯片编写单元测试? - How to write unit test for Angular Material Chips? angular材料cdk步进器如何使用构造函数 - How to use angular material cdk stepper with a constructor 如何编写 angular 材料对话框的单元测试用例 - How to write the unit test case for angular material dialog 如何在使用 Angular 材质选项卡的组件的单元测试中更改选项卡 - How to change tabs in the unit test of a component that uses Angular Material Tabs 如何更改 Angular 材料步进器动态创建的组件中的 state 图标? - How to change state icon in Angular material stepper dynamically created component? 如何使用角度6材料使自定义步进器组件可重复使用 - How to make customize stepper component reusable using angular 6 material 如何编写angular2的登录组件测试(单元测试) - How to Write test for login component for angular2 ( unit testing ) 如何在没有 TestBed 的情况下为 Angular 组件编写单元笑话测试? - How to write unit jest test for Angular Component without TestBed? 如何使用@input()为有角度的子组件编写单元测试 - how to write a unit test for angular child component with @input()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM