【问题标题】:angular 5 karma unit test: Failed: Cannot read property childcomponent property ( itgroup) of undefined角度5业力单元测试:失败:无法读取未定义的属性子组件属性(itgroup)
【发布时间】:2020-03-11 03:51:35
【问题描述】:

我正在尝试为在子组件中有一些依赖关系的父组件方法编写测试用例。

我收到以下错误。

失败:无法读取未定义的属性“itGroup”,这是我的子组件属性。

下面是我的代码 sn-p。

子组件。

child component:

---------------
----------------

interventionGroup: FormGroup;

 constructor() { }

  ngOnInit() {
    this.itGroup = new FormGroup({
      test1: new FormControl(''),
      test2: new FormControl(''),
    });
    }

父组件。

Parent Component mehod where i use the child component instance using view child
----------------------------
changeVal= false;
submitted() {
        this.isValidTemplate = this.dynamicTemplateComponent.itGroup.invalid;
        if (this.isValidTemplate) {
            //logic
        } else {
            //logic
        }
onChange(event){
this.changeval = true;
}        
        
test case for method which have the child component instance which is showing undefined:
---------------------------------------
  it('on submit hit service', async () => {
   

      component.onChange(event);
      component.changeval = true;
      component.submitted();
     const spy= spyOn(component, 'submitted');
      let button = fixture.debugElement.nativeElement.querySelector('#submitbutton');
      button.click();
      fixture.detectChanges();
      expect(component.submitted).toBeTruthy();

  });
<form [formGroup]="TemplateGroup" class="edjustMargin" style="margin-top:20px; width:700px">
      <div class="row">
        <div>
    <div class="col-sm-6">
      <label class="labelStyle ">dropdown:</label>
        <p-dropdown placeholder="Select val"  class="" [options]="Options" formControlName="dropdown" (onChange)="change($event)"></p-dropdown>
    </div>

<div *ngIf="changeVal">
<app-dynamic-template ></app-dynamic-template>
</div>
  
    <div  *ngIf="changeVal">          
    <button class="floating" id="submitbutton" type="submit" pButton  (click)="submitted()" label="Submit"></button>
    <button class="floating" *ngIf="fromPDialog" type="submit" pButton  (click)="cancelled()" label="Cancel"></button>
    <button class="floating" type="submit" pButton  (click)="resetClicked()" label="Clear"></button>
    </div>
  </form>

【问题讨论】:

    标签: javascript angular unit-testing asynchronous karma-jasmine


    【解决方案1】:

    你能告诉我你的整个测试吗?

    要获得子组件的句柄,请确保将子组件放入 declarations 数组中。

    import { ChildComponent } from 'childComponent';
    
    ...
    let component: ThisComponet;
    let childComponent: ChildComponent;
    .....
    declarations: [ChildComponent],
    .....
    component = fixture.componentInstance;
    // to get a handle on the child component, you can select it By.directive
    childComponent = fixture.debugElement.query(By.directive(ChildComponent)).componentInstance; 
    // should have access to your childComponent public properties and methods now
    

    【讨论】:

    • 非常感谢。我是否需要在我的测试文件中编写任何内容以反映 component.changeVal = true 并使用 querySelector 点击提交按钮方法?
    • 我认为你不应该这样做。在你的安排和行动之后,做一个fixture.detectChanges(),它应该被更新。
    • 您可以分享编辑后的示例吗?我也陷入了这种情况。
    • 我认为你最好在 StackOverflow 上提问并提供一个链接,然后我可以帮你举个例子?
    • 如果你的子组件是app-dynamic-template,那么是的,在做childComponent = fixture.debugElement.query(By.directive(ChildComponent)).componentInstance;之前,做component.changeVale = true;
    猜你喜欢
    • 2021-12-02
    • 2020-09-03
    • 2018-07-01
    • 1970-01-01
    • 2018-04-18
    • 2021-08-05
    • 2018-07-27
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多