我刚刚使用带有菜单的模板创建了一个带有Ionic cli的应用程序。
我已经设置了所有测试配置。我的TestBed看起来像这样
TestBed.configureTestingModule({
declarations: [MyApp],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
],
providers: [
{ provide: StatusBar, useFactory: () => StatusBarMock.instance() },
{ provide: SplashScreen, useFactory: () => SplashScreenMock.instance() },
],
});
模拟来自ionic-mocks
import { StatusBarMock, SplashScreenMock } from 'ionic-mocks';
我的测试通过了
beforeEach(() => {
fixture = TestBed.createComponent(MyApp);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('component is created', () => {
expect(component).toBeTruthy();
});
但我得到了这个“警告”:
错误:'未处理承诺拒绝:','未找到HomePage的组件工厂。你有没有把它添加到@ NgModule.entryComponents?',
HomePage
位于MyApp
的{{1}} @NgModule
上。我是否也必须将它包含在TestBed中?怎么样?
答案 0 :(得分:2)
只需将组件添加到条目组件:
TestBed.configureTestingModule({
declarations: [MyApp],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
],
entryComponents: [MyApp] <-------------------
});
或者您可以使用要测试的组件导入模块:
TestBed.configureTestingModule({
declarations: [MyApp],
imports: [
AppModuleWithHomePageComponent, <--------------
BrowserModule,
IonicModule.forRoot(MyApp),
],