我有这样的组件
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();
});
});
但是我总是会出错 TypeError:无法设置未定义的属性“ formGroup” 有人可以帮我解决我的错吗,谢谢?
答案 0 :(得分:1)
由于该属性没有默认值,请尝试执行此操作。
component.properties.formGroup = {
formGroup: new FormGroup({
fixed,
startTime,
endTime
}),
index: 0,
period: 'period'
}