[英]ng test template parse error 'ngb-carousel' is not a known element
[英]Element not known error in Karma when ran "ng test"
當我運行時,我的項目運行良好
服務
但是當我運行一個簡單的“tobeTruthy()”測試用例時,它顯示了多個錯誤
測試
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-menu”是核心模塊中的一個組件,核心模塊是在 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 { }
我該怎么做才能為應用程序模塊實例運行成功的測試用例?
Angular 開發人員經常對此感到困惑。 當您運行ng test
,karma 和 jasmine 會運行.spec.ts
文件中定義的 angular 模塊。 它根本不看你的正常代碼。 因此,無論您在app.module.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
。 這樣做是為了確保您的測試始終具有所需的定義。 如果您將另一個組件添加到AppModule
並在AppComponent
使用它,它也將在測試中可用。 此外,您不需要添加CUSTOM_ELEMENTS_SCHEMA
。
但是,您應該知道這將導入並創建您在app.component
使用的任何 3rd 方組件/服務。 有人會爭辯說這違背了單元測試的性質。 您仍然可以以某種方式模擬這些服務,但它們將被呈現。 此外,在角度應用程序中,在測試導入RouterModule
的模塊時,會使用RouterTestingModule
代替。 在測試中使用RouterModule
可能會弄亂您的測試,因為這些測試在無頭瀏覽器上運行並且RouterModule
可能會導致 URL 更改。
TestBed.configureTestingModule({
imports: [
AppModule
],
}).compileComponents();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.