在离子单元测试中找不到HomePage的组件工厂

时间:2018-01-22 22:15:48

标签: angular unit-testing ionic-framework

我刚刚使用带有菜单的模板创建了一个带有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中?怎么样?

1 个答案:

答案 0 :(得分:2)

只需将组件添加到条目组件:

TestBed.configureTestingModule({
  declarations: [MyApp],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
  ],
  entryComponents: [MyApp]  <-------------------
});

或者您可以使用要测试的组件导入模块:

TestBed.configureTestingModule({
  declarations: [MyApp],
  imports: [
    AppModuleWithHomePageComponent,  <--------------
    BrowserModule,
    IonicModule.forRoot(MyApp),
  ],