我有一个使用这个的构造函数:
constructor(
public core: CoreComponent,
) {
//
}
但是当我尝试使用这样的测试用例测试这些组件时:
describe('CoreSubMenuComponent', () => {
let component: CoreSubMenuComponent;
let fixture: ComponentFixture<CoreSubMenuComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
TestingModule.forRoot(),
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CoreSubMenuComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我收到一个空注射器错误:
Error: StaticInjectorError(DynamicTestModule)[CoreSubMenuComponent -> CoreComponent]:
StaticInjectorError(Platform: core)[CoreSubMenuComponent -> CoreComponent]:
NullInjectorError: No provider for CoreComponent!
它被导入到 testingModule 中,所以这不是问题。我也尝试将它添加到 providers 数组中。但这只是将其移动到无法添加到提供者数组的角度渲染器。我还在 中的@injectable()
标签之前添加了一个标签@component
,CoreComponent
但它不会改变我的测试失败。
我尝试使用它,@host()
但它会引发错误。我也试图用它来获取它,Injector
但这也会引发错误。这些是 4 个组件,它们将CoreComponent
用作 var 以从中获取一些信息,但它们使测试失败。我所有的其他测试都通过了 80 多个测试。仅这 4 个就行不通,因为它们试图获取CoreComponent
.
有人知道我如何用实例引用这个组件以便我的测试通过吗?
要修复此错误,请使用 @host en @optional 装饰器。这告诉编译器查找它,但如果未提供则不需要:
constructor(
@Host() @Optional() public core: CoreComponent,
) {
//
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句