假设我有一个简单的模块AppModule
,它有许多导入、声明和提供程序。现在我想为ListComponent
这个模块的声明列表中的一个组件编写一个测试。ListComponent
它本身使用了很多(但不是每个)导入的AppModule
. 我这样做:
import { TestBed } from '@angular/core/testing';
// +same copy-pasted list of imports from `AppModule`
beforeEach(done => {
TestBed.configureTestingModule({
imports: [
// +same copy-pasted list of imports from `AppModule`
],
declarations: [
// +same copy-pasted list of declarations from `AppModule`
],
providers: [
{
provide: Http,
useClass: HttpMock,
},
{
provide: Router,
useClass: RouterMock,
}
// +same copy-pasted list of providers from `AppModule`
]
});
它有效,但肯定是一种不正确的方法。我不想复制粘贴这么多。也许我可以以某种方便的方式重用 AppModule?伪代码如下:
let appModule = new AppModule();
beforeEach(done => {
TestBed.configureTestingModule({
imports: appModule.imports,
declarations: appModule.declarations,
providers: [...appModule.providers,
{
provide: Http,
useClass: HttpMock,
},
{
provide: Router,
useClass: RouterMock,
}
]
});
但我只是不知道/找不到这种方法的语法:(