单元测试错误:无法读取null的属性'controls'

问题描述 投票:0回答:1

我正在尝试运行简单的测试,但遇到以下问题:

TypeError: Cannot read property 'controls' of null
    at OvertimeComponent_ng_container_6_Template (ng:///OvertimeComponent.js:250:62)
    at executeTemplate 

我的模板代码:

<ng-container [formGroup]='shift'>
  <mat-card>
      <h6 class="text-left">Overtimes</h6>
    <div class="mt-2 mb-1">
      <mat-accordion multi="true">
        <ng-container *ngIf='shift.value'>
          <ng-container formArrayName="overtimes"
                        *ngFor="let overtime of shift.get('overtimes')['controls'];let i = index">
            <ng-container [formGroupName]='i'>
                <mat-expansion-panel>
... 

我用于测试的简单规格文件,只有一项测试应该创建组件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { OvertimeComponent } from './overtime.component';
import {FormBuilder, ReactiveFormsModule, Validators} from "@angular/forms";
import {MatDialogModule} from "@angular/material/dialog";

describe('OvertimeComponent', () => {
  let component: OvertimeComponent;
  let fixture: ComponentFixture<OvertimeComponent>;
  const formBuilder: FormBuilder = new FormBuilder();

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ OvertimeComponent ],
      imports: [ReactiveFormsModule, MatDialogModule],
      providers: [ { provide: FormBuilder, useValue: formBuilder } ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(OvertimeComponent);
    component = fixture.componentInstance;
    component.shift = formBuilder.group({
      chain: ['chain', Validators.required],
      ip: [
            '',
            Validators.required
      ],
      action: ['action', Validators.required]
    });

    fixture.detectChanges();
  });

  it('should create', () => {
      expect(component).toBeTruthy();
  });
});

感谢您为解决此问题提供的任何帮助!预先感谢!

karma-jasmine angular9
1个回答
0
投票
<ng-container formArrayName="overtimes" *ngFor="let overtime of shift.get('overtimes')['controls'];let i = index">
© www.soinside.com 2019 - 2024. All rights reserved.