如何在 Angular 组件的单元测试中提供 ControlContainer?
How do I provide ControlContainer inside an Angular component's unit test?
根据标题,如何在 Angular 单元测试中提供 ControlContainer
?
产生的错误如下:
NullInjectorError: StaticInjectorError(DynamicTestModule)[ChildFormComponent -> ControlContainer]:
StaticInjectorError(Platform: core)[ChildFormComponent -> ControlContainer]:
NullInjectorError: No provider for ControlContainer!
我尝试提供ControlContainer
:
beforeEach(() => {
const parentFixture = TestBed.createComponent(ParentFormComponent);
const parentComponent = parentFixture.componentInstance;
parentComponent.initForm();
fixture = TestBed.createComponent(ChildFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
我看到其他解决方案使用:
declarations: [
...MY_DECLARATIONS
],
imports: [
...MY_IMPORTS
],
providers: [
{
provide: ControlContainer,
useValue: MyFormGroupDirective
}
]
我运气不好,但如果这里有人能够阐明一些问题;我将不胜感激。
我能够从这个 post:
中得出解决方案
我最后做的是:
let component: MyChildComponent;
let fixture: ComponentFixture<MyChildComponent>
let fg: FormGroup;
let fgd: FormGroupDirective;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ ...MY_DECLARATIONS ],
imports: [ ...MY_IMPORTS ],
providers: [
{ provide: ControlContainer, useValue: fgd }
]
});
});
beforeEach(() => {
const parentFixture = TestBed.createComponent(MyParentComponent);
const parentComponent = parentFixture.componentInstance;
fg = parentComponent.form;
fgd = new FormGroupDirective([], []);
fgd.form = fg;
const childFixture = TestBed.createComponent(MyChildComponent);
const childComponent = childFixture.componentInstance;
childFixture.detectChanges();
});
我想做的是将初始化的父表单组附加到子项。
根据标题,如何在 Angular 单元测试中提供 ControlContainer
?
产生的错误如下:
NullInjectorError: StaticInjectorError(DynamicTestModule)[ChildFormComponent -> ControlContainer]:
StaticInjectorError(Platform: core)[ChildFormComponent -> ControlContainer]:
NullInjectorError: No provider for ControlContainer!
我尝试提供ControlContainer
:
beforeEach(() => {
const parentFixture = TestBed.createComponent(ParentFormComponent);
const parentComponent = parentFixture.componentInstance;
parentComponent.initForm();
fixture = TestBed.createComponent(ChildFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
我看到其他解决方案使用:
declarations: [
...MY_DECLARATIONS
],
imports: [
...MY_IMPORTS
],
providers: [
{
provide: ControlContainer,
useValue: MyFormGroupDirective
}
]
我运气不好,但如果这里有人能够阐明一些问题;我将不胜感激。
我能够从这个 post:
中得出解决方案我最后做的是:
let component: MyChildComponent;
let fixture: ComponentFixture<MyChildComponent>
let fg: FormGroup;
let fgd: FormGroupDirective;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ ...MY_DECLARATIONS ],
imports: [ ...MY_IMPORTS ],
providers: [
{ provide: ControlContainer, useValue: fgd }
]
});
});
beforeEach(() => {
const parentFixture = TestBed.createComponent(MyParentComponent);
const parentComponent = parentFixture.componentInstance;
fg = parentComponent.form;
fgd = new FormGroupDirective([], []);
fgd.form = fg;
const childFixture = TestBed.createComponent(MyChildComponent);
const childComponent = childFixture.componentInstance;
childFixture.detectChanges();
});
我想做的是将初始化的父表单组附加到子项。