【发布时间】: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