当我运行时,我的项目运行良好。
吴服务
但是,当我运行一个简单的"tobeTruthy()“测试用例时,它显示了多个错误
ng试验
HTML文件
<ngx-spinner bdColor="rgba(51,51,51,0.8)" size="medium" color="#fff" type="ball-scale-multiple">
<p style="font-size: 20px; color: white">Loading...</p>
</ngx-spinner>
<div *ngIf="isAuthenticated" class="container-fluid">
<app-app-menu></app-app-menu>
<router-outlet></router-outlet>
</div>app.component.ts
import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { map } from 'rxjs/operators';
import { AppState } from './app.reducer';
import { UserState } from './core/store/core.state';
import * as CoreActions from './core/store/core.actions';
import { Globals } from './shared/globals';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
datetime = new Date();
title = 'curemd-x';
isAuthenticated = false;
constructor(private store: Store<AppState>, private router: Router,
private globals: Globals) {}
...
...因果报应错误
"Failed: Template parse errors:
'ngx-spinner' is not a known element:
1. If 'ngx-spinner' is an Angular component, then verify that it is part of this module.
2. If 'ngx-spinner' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<ngx-spinner bdColor="rgba(51,51,51,0.8)" size="medium" color="#fff" type="ball-scale-multiple">
<p"): ng:///DynamicTestModule/AppComponent.html@0:0
'app-app-menu' is not a known element:
1. If 'app-app-menu' is an Angular component, then verify that it is part of this module.
2. If 'app-app-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
</ngx-spinner>
<div *ngIf="isAuthenticated" class="container-fluid">
[ERROR ->]<app-app-menu></app-app-menu>
<router-outlet></router-outlet>
</div>
"): ng:///DynamicTestModule/AppComponent.html@4:2我也尝试过包括"CUSTOM_ELEMENTS_SCHEMA“,但没有起作用。
“app-app菜单”是核心模块中的一个组件,核心模块是在app.module中导入的。
app.module.ts
declarations: [
AppComponent,
FirstOrDefaultPipe
],
imports: [
RouterModule,
BrowserModule,
HttpClientModule,
PatientModule,
StoreModule.forRoot(AppReducers),
EffectsModule.forRoot([]),
CoreModule,
NgxSpinnerModule,
BrowserAnimationsModule,
DropDownsModule
],
providers: [Globals],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule { }为了成功地运行应用程序模块实例的测试用例,我可以做些什么?
发布于 2019-05-03 09:14:35
棱角分明的开发人员常常对此感到困惑。当您运行ng test时,业力和茉莉运行在.spec.ts文件中定义的角模块。它根本不看您的正常代码。因此,无论您在app.module.ts中输入什么,都不会对您的测试模块产生任何影响。你可以做两件事。
CUSTOM_ELEMENTS_SCHEMA
在app.component.spec.ts中执行以下操作 TestBed.configureTestingModule({
declarations: [
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();这将解决您现在所遇到的错误。但是,您可能会遇到其他人,因为我已经看到您向AppComponent注入了一些服务,这使我们看到了下一步您可以做的事情
AppModule。
您可以在AppModule中导入TestBed,如下所示。这将确保您的测试始终具有所需的定义。如果将另一个组件添加到AppModule中并在AppComponent中使用,它也将在测试中可用。另外,您也不需要添加CUSTOM_ELEMENTS_SCHEMA。
但是,您应该知道这将导入和创建您在app.component中使用的任何第三方组件/服务。有人会认为这违背了单元测试的本质。您仍然可以在某种程度上嘲弄这些服务,但它们将被提供。此外,在角应用程序中,在测试导入RouterModule的模块时,将使用RouterTestingModule。在测试中使用RouterModule可能会使您的测试变得一团糟,因为这些测试运行在无头浏览器上,而RouterModule可能会导致URL更改。 TestBed.configureTestingModule({
imports: [
AppModule
],
}).compileComponents();https://stackoverflow.com/questions/55966510
复制相似问题