【问题标题】:No component factory found for HomePage in Ionic Unit Test在离子单元测试中找不到主页的组件工厂
【发布时间】:2018-07-01 15:32:51
【问题描述】:

我刚刚使用带有菜单的模板创建了一个带有 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();
});

但我收到了这个“警告”:

错误:'未处理的承诺拒绝:','未找到主页的组件工厂。你把它添加到@NgModule.entryComponents 了吗?',

HomePage 位于 MyApp@NgModuleentryComponents。我是否也必须将它包含在 TestBed 中?怎么样?

【问题讨论】:

    标签: angular unit-testing ionic-framework


    【解决方案1】:

    只需将组件添加到入口组件:

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

    或者您可以导入带有您想要测试的组件的模块:

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

    【讨论】:

    • 没有页面模块,至少在ionic start sidemenu生成的应用程序中没有......但这是有道理的,我稍后会尝试创建它。我猜 Ionic 生成的应用程序都不关心测试......
    • @BrunoLM,我已经更新了我的答案。只需在测试模块的入口组件中添加MyApp即可
    猜你喜欢
    • 2020-12-05
    • 2019-07-26
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多