我正在测试我的 DashboardComponent
,这是我的 beforeEach
block :
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
routing
],
declarations: [
DashboardComponent,
Ellipsis,
FavouritesComponent,
FormatDuration,
LoginComponent,
SearchComponent
],
});
this.fixture = TestBed.createComponent(DashboardComponent);
});
它已经有一些与仪表板无关的导入(比如 LoginComponent)。但它一直要求更多的进口:
Error: Component **** is not part of any NgModule or the module has not been imported into your module.
app.module.ts
中的所有内容都存在,所以这不是问题。
是什么导致了这种行为,我该如何解决?
最佳答案
将 AppModule
和 RouterTestingModule
添加到您传递给 TestBed.configureTestingModule()
的对象的 imports
中。
不确定在导入 RouterTestingModule
时是否还需要添加 {provide: APP_BASE_HREF, useValue: '/'}
。
另请参阅此 PR 以获取即将发布的测试文档 https://github.com/angular/angular.io/pull/2198/files
关于angular - karma 不断要求更多与测试组件无关的进口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39447670/