Angular TypeError:无法设置属性';formGroup';未定义的
我有一些像这样的组件Angular TypeError:无法设置属性';formGroup';未定义的,angular,typescript,unit-testing,jasmine,Angular,Typescript,Unit Testing,Jasmine,我有一些像这样的组件 import { Component, Input } from '@angular/core'; import { WorkingHours } from '../../../../../hours'; import { FormGroup } from '@angular/forms'; @Component({ selector: '[app-working-hours]', templateUrl: './working-hours.component.h
import { Component, Input } from '@angular/core';
import { WorkingHours } from '../../../../../hours';
import { FormGroup } from '@angular/forms';
@Component({
selector: '[app-working-hours]',
templateUrl: './working-hours.component.html',
styleUrls: ['./working-hours.component.scss']
})
export class WorkingHoursComponent {
@Input() properties: { formGroup: FormGroup; workingHours: WorkingHours; index: number; period: string; };
get formControls(): any { return this.properties.formGroup.controls; }
constructor() {
}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { WorkingHoursComponent } from './working-hours.component';
import { ReactiveFormsModule, FormControl, FormGroup } from '@angular/forms';
describe('WorkingHoursComponent', () => {
let component: WorkingHoursComponent;
let fixture: ComponentFixture<WorkingHoursComponent>;
beforeEach(async(() => {
imports.push(ReactiveFormsModule);
TestBed.configureTestingModule({
imports: imports,
declarations: [
WorkingHoursComponent
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(WorkingHoursComponent);
component = fixture.componentInstance;
let fixed = new FormControl(false);
let startTime = new FormControl('');
let endTime = new FormControl('');
component.properties.formGroup = new FormGroup({
fixed,
startTime,
endTime
});
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
然后像这样测试
import { Component, Input } from '@angular/core';
import { WorkingHours } from '../../../../../hours';
import { FormGroup } from '@angular/forms';
@Component({
selector: '[app-working-hours]',
templateUrl: './working-hours.component.html',
styleUrls: ['./working-hours.component.scss']
})
export class WorkingHoursComponent {
@Input() properties: { formGroup: FormGroup; workingHours: WorkingHours; index: number; period: string; };
get formControls(): any { return this.properties.formGroup.controls; }
constructor() {
}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { WorkingHoursComponent } from './working-hours.component';
import { ReactiveFormsModule, FormControl, FormGroup } from '@angular/forms';
describe('WorkingHoursComponent', () => {
let component: WorkingHoursComponent;
let fixture: ComponentFixture<WorkingHoursComponent>;
beforeEach(async(() => {
imports.push(ReactiveFormsModule);
TestBed.configureTestingModule({
imports: imports,
declarations: [
WorkingHoursComponent
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(WorkingHoursComponent);
component = fixture.componentInstance;
let fixed = new FormControl(false);
let startTime = new FormControl('');
let endTime = new FormControl('');
component.properties.formGroup = new FormGroup({
fixed,
startTime,
endTime
});
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从'@angular/core'导入{NO_ERRORS_SCHEMA};
从“./working hours.component”导入{working hours component};
从'@angular/forms'导入{ReactiveFormsModule,FormControl,FormGroup};
描述('WorkingHoursComponent',()=>{
let组件:工作时间组件;
let夹具:组件夹具;
beforeach(异步(()=>{
imports.push(ReactiveFormsModule);
TestBed.configureTestingModule({
进口:进口,,
声明:[
工作时间组件
],
架构:[无错误\u架构]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(工作时间组件);
组件=fixture.componentInstance;
let fixed=新表单控件(false);
让startTime=newformcontrol(“”);
让endTime=新表单控件(“”);
component.properties.formGroup=新的formGroup({
固定的,
开始的时候,
结束时间
});
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});
但我总是犯错误
TypeError:无法设置未定义的属性“formGroup”
有人能告诉我哪里错了吗,谢谢?因为属性没有默认值,请尝试此操作
component.properties.formGroup={
formGroup:新formGroup({
固定的,
开始的时候,
结束时间
}),
索引:0,
句号:“句号”
}