我尝试编写一些测试,得到以下错误消息:错误:NG0302:找不到管道"keyvalue"!。更多信息,请访问https://angular.io/errors/NG0302.
这个keyValue管道在CommonModule中,所以我将这个模块导入到spec.ts文件中,但它仍然不起作用。
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
CommonModule,
RouterTestingModule,
HttpClientTestingModule,
NotifierModule,
ReactiveFormsModule
],
providers: [ConfigureDeviceComponent]
});
fixture = TestBed.createComponent(ConfigureDeviceComponent);
component = fixture.componentInstance;
component.ngOnInit();
});
有人有什么建议吗?非常感谢。
FIX:执行以下更改:
- 在本地模块中,在
declarations
中添加KeyValue
管道 - 在
spec
文件中,在declarations
中添加KeyValue
管道的类名 - 如果出现类似
NullInjectorError: R3InjectorError(DynamicTestModule)[PipeName -> PipeName]: NullInjectorError: No provider for PipeName!
的错误,也可以添加providers
出现错误的本地规范文件
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
CommonModule,
RouterTestingModule,
HttpClientTestingModule,
NotifierModule,
ReactiveFormsModule
],
providers: [
ConfigureDeviceComponent
],
declarations: [
// Add your pipe here
]
});
fixture = TestBed.createComponent(ConfigureDeviceComponent);
component = fixture.componentInstance;
component.ngOnInit();
});
当前组件所在的本地模块文件
@NgModule({
declarations: [...], Include your pipe here
imports: [...],
providers: [...],
bootstrap: [AppComponent]
})
你似乎试图提供一个组件,而不是声明它。也许这就是angular认为它是一个管道的原因。尝试将providers
更改为declarations
我不确定这是正确的答案,所以让我知道它是否有效