Angular9中使用FormGroup时测试引擎失败
Test engine failure when FormGroup is used in Angular 9
我有以下组件:
export class BaseFormComponent implements OnInit {
basicFormGroup: FormGroup;
constructor(
protected basicProviderService: BasicProviderService,
protected formBuilder: FormBuilder,
protected dialog: MatDialog,
protected spinner: NgxSpinnerService
) {
this.basicFormGroup = this.formBuilder.group({
name:[''],
note: ['', Validators.required],
description: ['', Validators.required]
});
}
}
其模板代码:
<h3><span>Basic form data</span></h3>
<mat-divider></mat-divider>
<form *ngIf="true" [formGroup]="basicFormGroup">
<!-- its content does not matter-->
</form>
这是测试:
fdescribe('BasicFormComponent', () => {
let component: BasicFormComponent;
let fixture: ComponentFixture<BasicFormComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [BasicFormComponent],
imports: [
FormsModule,
MatDialogModule,
MatAutocompleteModule,
MatSnackBarModule,
MatExpansionModule,
MatStepperModule,
MatFormFieldModule,
TextFieldModule,
MatInputModule,
MatDatepickerModule,
MatNativeDateModule,
MatListModule,
MatSelectModule,
HttpClientTestingModule,
RouterTestingModule,
BrowserAnimationsModule,
CoreModule
],
providers: [
{ provide: BasicProviderService, useClass: BasicProviderServiceStub }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BasicFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
当它运行时,调用 createComponent
方法时出现以下错误:
HeadlessChrome 80.0.3987 (Windows 10.0.0) BasicFormComponent should create FAILED
Error: NodeInjector: NOT_FOUND [ControlContainer]
at getOrCreateInjectable (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5467:1)
at Module.ɵɵdirectiveInject (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:20759:1)
at NodeInjectorFactory.NgControlStatusGroup_Factory [as factory] (http://localhost:9877/_karma_webpack_/node_modules/@angular/forms/__ivy_ngcc__/fesm2015/forms.js:1013:135)
at getNodeInjectable (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5598:1)
at instantiateAllDirectives (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:12645:1)
at createDirectivesInstances (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11881:1)
at ɵɵelementStart (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:20943:1)
at BasicFormComponent_Template (ng:///BasicFormComponent.js:192:9)
at executeTemplate (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11841:1)
at renderView (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11627:1)
Expected undefined to be truthy.
at UserContext.<anonymous> (http://localhost:9877/_karma_webpack_/src/app/basic-form/basic-form.component.spec.ts:62:23)
at ZoneDelegate.invoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
at ProxyZoneSpec.onInvoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:305:1)
at ZoneDelegate.invoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
HeadlessChrome 80.0.3987 (Windows 10.0.0): Executed 1 of 181 (1 FAILED) (0 secs / 0.293 secs)
我尝试了以下方法:
- 当我使用
ngIf='false'
时,测试成功 --> 所以它可能因为形式 而失败
- 当我将
[formGroup]='basicFormGroup'
属性 替换为 [formGroup]='undefined'
时,测试失败 --> 因此它以 any 形式失败
app.module.ts 文件包含 FormsModule
导入。
看来测试引擎找不到表单的抽象基。
这可能是什么原因造成的?
反应式表单需要 ReactiveFormsModule
才能导入,模板驱动表单需要 FormsModule
.
此处您使用的是 formGroup
指令,它是反应形式的一部分,因此需要 ReactiveFormsModule
。
参考angular docs这两种形式的区别
我有以下组件:
export class BaseFormComponent implements OnInit {
basicFormGroup: FormGroup;
constructor(
protected basicProviderService: BasicProviderService,
protected formBuilder: FormBuilder,
protected dialog: MatDialog,
protected spinner: NgxSpinnerService
) {
this.basicFormGroup = this.formBuilder.group({
name:[''],
note: ['', Validators.required],
description: ['', Validators.required]
});
}
}
其模板代码:
<h3><span>Basic form data</span></h3>
<mat-divider></mat-divider>
<form *ngIf="true" [formGroup]="basicFormGroup">
<!-- its content does not matter-->
</form>
这是测试:
fdescribe('BasicFormComponent', () => {
let component: BasicFormComponent;
let fixture: ComponentFixture<BasicFormComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [BasicFormComponent],
imports: [
FormsModule,
MatDialogModule,
MatAutocompleteModule,
MatSnackBarModule,
MatExpansionModule,
MatStepperModule,
MatFormFieldModule,
TextFieldModule,
MatInputModule,
MatDatepickerModule,
MatNativeDateModule,
MatListModule,
MatSelectModule,
HttpClientTestingModule,
RouterTestingModule,
BrowserAnimationsModule,
CoreModule
],
providers: [
{ provide: BasicProviderService, useClass: BasicProviderServiceStub }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BasicFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
当它运行时,调用 createComponent
方法时出现以下错误:
HeadlessChrome 80.0.3987 (Windows 10.0.0) BasicFormComponent should create FAILED
Error: NodeInjector: NOT_FOUND [ControlContainer]
at getOrCreateInjectable (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5467:1)
at Module.ɵɵdirectiveInject (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:20759:1)
at NodeInjectorFactory.NgControlStatusGroup_Factory [as factory] (http://localhost:9877/_karma_webpack_/node_modules/@angular/forms/__ivy_ngcc__/fesm2015/forms.js:1013:135)
at getNodeInjectable (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:5598:1)
at instantiateAllDirectives (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:12645:1)
at createDirectivesInstances (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11881:1)
at ɵɵelementStart (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:20943:1)
at BasicFormComponent_Template (ng:///BasicFormComponent.js:192:9)
at executeTemplate (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11841:1)
at renderView (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11627:1)
Expected undefined to be truthy.
at UserContext.<anonymous> (http://localhost:9877/_karma_webpack_/src/app/basic-form/basic-form.component.spec.ts:62:23)
at ZoneDelegate.invoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:365:1)
at ProxyZoneSpec.onInvoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:305:1)
at ZoneDelegate.invoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
HeadlessChrome 80.0.3987 (Windows 10.0.0): Executed 1 of 181 (1 FAILED) (0 secs / 0.293 secs)
我尝试了以下方法:
- 当我使用
ngIf='false'
时,测试成功 --> 所以它可能因为形式 而失败
- 当我将
[formGroup]='basicFormGroup'
属性 替换为[formGroup]='undefined'
时,测试失败 --> 因此它以 any 形式失败
app.module.ts 文件包含 FormsModule
导入。
看来测试引擎找不到表单的抽象基。
这可能是什么原因造成的?
反应式表单需要 ReactiveFormsModule
才能导入,模板驱动表单需要 FormsModule
.
此处您使用的是 formGroup
指令,它是反应形式的一部分,因此需要 ReactiveFormsModule
。
参考angular docs这两种形式的区别