我有一個具有一些共享功能的“普通”(沒什么特別的)Angular 應用程式。因為我想避免在組件測驗中一遍又一遍地嘲笑這些共享功能,所以我正在考慮為每個功能引入一個測驗模塊版本。它應該看起來像這樣:
SharedModule
|
|- drop-zone
| |_ (module contents skipped for brevity)
| |_ drop-zone.module.ts
| |_ drop-zone-testing.module.ts
|
|- other-features...
- 所述
drop-zone.module.ts應的角模塊,包括所有的“刺/正常”declarations,services等等。 - 例如,
drop-zone-testing.module.ts還應包括“正常”declarations但虛假的服務。
但是這樣做時,我在運行時收到 Angular 錯誤ng build:
The Component 'FileDropZoneComponent' is declared by more than one NgModule.
- 'FileDropZoneComponent' is listed in the declarations of the NgModule 'FileDropTestingModule'.
- 'FileDropZoneComponent' is listed in the declarations of the NgModule 'FileDropModule'.
我已經嘗試過洗掉所有testing-module通過從模式的角度,構建-files*testing.module.ts中tsconfig的exclude,但無濟于事:/我以為,從構建不包括這些模塊應該抱怨兩個模塊存在具有相同的宣告停轉。但它不起作用。
有沒有人有解決方案如何為 Angular 應用程式中的自定義功能創建 TestingModules?
uj5u.com熱心網友回復:
我想出了如何做到這一點 - 但它似乎是一種對我來說甚至沒有太大意義的解決方法:D但它在測驗和構建中都有效:
我應該提到,在我的解決方案中,我仍然洗掉了該檔案,我在其中定義了自己的裝飾器tsconfig.app.json:
{
"exclude": ["src/**/testing/**/*"],
}
- 創建你自己的裝飾器,簡單地包裝
NgModule裝飾器:
// e.g. src/shared/testing/utils.ts
export function NgTestingModule(obj: NgModule) {
return NgModule(obj);
}
- 在您的測驗模塊中使用:
import { NgTestingModule } from '../testing/utils';
@NgTestingModule({ ... })
export class DropZoneTestingModule {}
- 并在您的測驗中:
TestBed.configureTestingModule({
imports: [DropZoneTestingModule],
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/350405.html
上一篇:為什么Perl的Devel::Cover認為有些分支和條件沒有覆寫?
下一篇:與Pytest模塊的后期系結沖突
