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



我刚刚使用带有菜单的模板创建了一个带有 Ionic cli 的应用程序。

我已经设置了所有测试配置。我的测试平台看起来像这样

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();
});

但是我收到这个"警告":

错误:"未处理的承诺拒绝:", "找不到主页的组件工厂。您是否将其添加到@NgModule.entryComponents?

HomePageMyApp @NgModule entryComponents上。我是否也必须将其包含在测试平台中?如何?

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

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

或者,您可以导入包含要测试的组件的模块:

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

相关内容

最新更新