[英]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
}
]
我沒有運氣,但如果這里有人能夠闡明; 我會深深地感激它。
我能夠從這篇文章中得出解決方案:
如何在角度單元測試中模擬 ControlContainer?
我最終做的是:
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();
});
我想做的是將初始化的父表單組附加到子表單組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.