Angular Karma不断要求更多与测试组件无关的进口产品
我正在测试我的Angular Karma不断要求更多与测试组件无关的进口产品,angular,karma-jasmine,Angular,Karma Jasmine,我正在测试我的仪表板组件,这是我的在每个块之前: beforeEach(() => { TestBed.configureTestingModule({ imports: [ FormsModule, routing ], declarations: [ DashboardComponent, Ellipsis, FavouritesComponent,
仪表板组件
,这是我的在每个
块之前:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
routing
],
declarations: [
DashboardComponent,
Ellipsis,
FavouritesComponent,
FormatDuration,
LoginComponent,
SearchComponent
],
});
this.fixture = TestBed.createComponent(DashboardComponent);
});
它已经有了一些与仪表板无关的导入(比如LoginComponent)。但它不断要求更多的进口:
错误:组件****不是任何NGM模块的一部分,或者该模块具有
尚未导入到您的模块中
app.module.ts
中的所有内容都存在,因此这不是问题所在
导致这种行为的原因是什么?我如何解决它?将
AppModule
和RouterTestingModule
添加到导入传递到测试床的对象的。configureTestingModule()
不确定导入RouterTestingModule
时是否仍需要添加{provide:APP_BASE_HREF,useValue:'/'}
关于即将发布的测试文档,请参见此PR,因为事实证明,对于2.1.x Angular版本,您不想添加AppModule,而是我必须删除单独的路由模块以消除此错误并使测试成功运行
我的测试规范最终是这样的:
import {TestBed} from '@angular/core/testing';
import {RouterTestingModule} from '@angular/router/testing';
import {FormsModule} from '@angular/forms';
import {HttpModule, JsonpModule} from '@angular/http';
import {TypeaheadModule} from '../../node_modules/ng2-bootstrap/components/typeahead';
import {AppComponent} from './app.component';
describe('AppComponent', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule,
HttpModule,
JsonpModule,
TypeaheadModule
],
declarations: [AppComponent]
}));
it('should instantiate the AppComponent', () => {
let fixture = TestBed.createComponent(AppComponent);
expect(fixture.componentInstance instanceof AppComponent).toBe(true, 'should create AppComponent');
});
});
如何将这些组件已经是其成员的模块添加到导入:[…]
?我不确定你的意思,比如导入app.component?我试过了,但没有成功,导入BrowserModule也没有解决任何问题。我的意思是将MyFeatureModule
添加到imports:[…]
(其中MyFeatureModule
是包含“组件****”的模块。它不属于另一个模块。它怎么可能不属于另一个模块?每个组件都只属于一个模块,否则您无法在Angular2应用程序中使用它。我也有同样的问题,但按建议添加模块并不能解决问题。2.1.x有什么变化吗?