简体   繁体   English

导入角度测试

[英]Imports on angular tests

I have an angular project and I have a component to test.Now this componnet has a submodule.ts and the project has a general module.ts and also there is a test.module.ts. 我有一个有角度的项目,有一个要测试的组件。现在这个组件有一个submodule.ts,项目有一个通用的module.ts,还有一个test.module.ts。 An example of each one of them: 其中每个示例:

submodule.ts that is a module shared for some componnets. submodule.ts,是一些组件共享的模块。

 import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { RouterModule } from '@angular/router'; import { OncosupSharedModule } from '../../shared'; import { ProtocoloService, ProtocoloPopupService, ProtocoloComponent, ProtocoloDetailComponent, ProtocoloDialogComponent, //testing only this componnet ProtocoloPopupComponent, ProtocoloDeletePopupComponent, ProtocoloDeleteDialogComponent, protocoloRoute, protocoloPopupRoute, ProtocoloResolvePagingParams, } from './'; const ENTITY_STATES = [ ...protocoloRoute, ...protocoloPopupRoute, ]; @NgModule({ imports: [ OncosupSharedModule, RouterModule.forChild(ENTITY_STATES) ], declarations: [ ProtocoloComponent, ProtocoloDetailComponent, ProtocoloDialogComponent, ProtocoloDeleteDialogComponent, ProtocoloPopupComponent, ProtocoloDeletePopupComponent, ], entryComponents: [ ProtocoloComponent, ProtocoloDialogComponent, //I am only testing this componnet ProtocoloPopupComponent, ProtocoloDeleteDialogComponent, ProtocoloDeletePopupComponent, ], providers: [ ProtocoloService, ProtocoloPopupService, ProtocoloResolvePagingParams, ], schemas: [CUSTOM_ELEMENTS_SCHEMA] 

my general.module.ts for all teh project 我对所有项目的general.module.ts

 @NgModule({ imports: [ BrowserModule, OncosupAppRoutingModule, Ng2Webstorage.forRoot({ prefix: 'jhi', separator: '-'}), OncosupSharedModule, OncosupHomeModule, OncosupAdminModule, OncosupAccountModule, OncosupEntityModule, FormsModule, ReactiveFormsModule, // jhipster-needle-angular-add-module JHipster will add new module here ], declarations: [ JhiMainComponent, NavbarComponent, ErrorComponent, PageRibbonComponent, ActiveMenuDirective, FooterComponent ], providers: [ ProfileService, PaginationConfig, UserRouteAccessService, { provide: HTTP_INTERCEPTORS, useClass: AuthExpiredInterceptor, multi: true, deps: [ StateStorageService, Injector ] }, { provide: HTTP_INTERCEPTORS, useClass: ErrorHandlerInterceptor, multi: true, deps: [ JhiEventManager ] }, { provide: HTTP_INTERCEPTORS, useClass: NotificationInterceptor, multi: true, deps: [ Injector ] } ], bootstrap: [ JhiMainComponent ], schemas: [CUSTOM_ELEMENTS_SCHEMA] }) export class OncosupAppModule {} 

And my test.module for tests only: 而我的test.module仅用于测试:

 @NgModule({ providers: [ DatePipe, JhiDataUtils, JhiDateUtils, JhiParseLinks, { provide: JhiLanguageService, useClass: MockLanguageService }, { provide: JhiLanguageHelper, useClass: MockLanguageHelper }, { provide: JhiEventManager, useClass:  MockEventManager }, { provide: NgbActiveModal, useClass: MockActiveModal }, { provide: ActivatedRoute, useValue: new MockActivatedRoute({id: 123}) }, { provide: Router, useClass: MockRouter }, { provide: Principal, useClass: MockPrincipal }, { provide: AccountService, useClass: MockAccountService }, { provide: LoginModalService, useValue: null }, { provide: ElementRef, useValue: null }, { provide: Renderer, useValue: null }, { provide: JhiAlertService, useValue: null }, { provide: NgbModal, useValue: null }, ], imports: [HttpClientTestingModule, FormsModule, ReactiveFormsModule, RouterModule, RouterTestingModule ], schemas: [ CUSTOM_ELEMENTS_SCHEMA ] }) 

Now I want to test only one of the compoonets that is sharing the submodule.ts. 现在,我只想测试共享submodule.ts的compoonets之一。 Providers,schemas and imports yess can be imported but entryComponents for example give errors. 提供程序,方案和导入yes可以导入,但是entryComponents例如会出错。 I was wondering if there is any rule on what should be imported and from which module?? 我想知道是否有什么规则应该导入什么以及从哪个模块导入?

In your subcomponent, only import the modules that you component use. 在子组件中,仅导入组件使用的模块。

if you import your big modules, you will import every module declared in it, along with every component, provider, directive, pipe (etc.) that are in it . 如果导入大模块,则将导入其中声明的每个模块以及其中的每个组件,提供程序,指令,管道(等) Imagine the time to mock all of it ... 想象一下模拟所有这些的时间...

For the entryComponents , there's a special syntax. 对于entryComponents ,有一种特殊的语法。 I found it into Angualr Material's modal test, and I frankly don't want to search for the source now, so you'll have to take my word for it : 我在Angualr Material的模态测试中找到了它,坦率地说,我现在不想搜索源,因此您必须相信我的想法:

TestBed
  .configureTestingModule(...)
  .overrideModule(BrowserDynamicTestingModule, { set: { entryComponents: [UploadModalComponent] } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM