【问题标题】:ng test angular 5 failsng test angular 5 失败
【发布时间】:2018-12-01 19:21:25
【问题描述】:

我正在尝试使用 Karma 在我的 angular 5 应用程序上执行测试,但是对于 10 个组件,我基本上有相同的错误: AccountsComponent 应该创建

失败:模板解析错误:“app-wizard-cmp-edit”不是已知的 元素: 1. 如果 'app-wizard-cmp-edit' 是一个 Angular 组件,那么验证它是这个模块的一部分。 2. 如果 'app-wizard-cmp-edit' 是一个 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到该组件的 '@NgModule.schemas' 压制此消息。 (" [错误->] "): ng:///DynamicTestModule/AccountsComponent.html@51:8

当我像这样在网上搜索解决方案时How to import component into another root component in Angular 2 它没有解决我的问题 以上是我的帐户模块中的导入:

    @NgModule({
    imports: [
        CommonModule,
        RouterModule.forChild(AccountsRoutes),
        FormsModule,
        ReactiveFormsModule,
        NouisliderModule,
        TagInputModule,
        MaterialModule, SharedModule
    ],
    declarations: [AccountsComponent, WizardComponent],
    exports: [WizardComponent]
    , providers: [AccountslistService],

})
export class AccountsModule {
}

这里是 ngModule:

@NgModule({
    imports: [
        CommonModule,
        BrowserAnimationsModule,
        FormsModule,
        HttpModule,
        HttpClientModule,
        RouterModule.forRoot(AppRoutes),
        MaterialModule,
        MatNativeDateModule,
        SidebarModule,
        NavbarModule,
        FooterModule,
        FixedpluginModule,
    ],
    declarations: [
        AppComponent,
        AdminLayoutComponent,
        AuthLayoutComponent,
        AccountsComponent

    ],
    bootstrap: [AppComponent]
    , providers: [AuthGuard, AuthenticationService, ChatAlertsService, Configuration]
})
export class AppModule {
}

我真的被卡住了,因为我不知道为什么测试失败但是当我构建它时应用程序运行正常

【问题讨论】:

  • 如果您为任何一个失败的组件提供测试用例将会很有帮助。很可能您在 ConfigureTestingModule 函数的声明中没有当前组件或子组件。
  • 我只是按照我在官方在线指南上找到的有关如何测试角度应用程序的步骤

标签: angular karma-jasmine


【解决方案1】:

您可以将schemas: [NO_ERRORS_SCHEMA] 添加到您的测试组件配置中,这将简单地忽略与child component 标签相关的错误。

TestBed.configureTestingModule({
      declarations: [ MyComponent],
      schemas: [NO_ERRORS_SCHEMA]
      // other stuff
})

但在使用时要小心,因为它可能会隐藏任何其他错误,并导致难以调试失败的测试用例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2020-09-03
    • 2023-03-21
    相关资源
    最近更新 更多