单元测试 – 如何知道单元测试时要导入哪些组件?
我正在使用Angular2(2.1.0)最终版本.
我在使用单元测试时通过AppModule导入所有组件… beforeEach(async(() => { TestBed.configureTestingModule({ imports: [AppModule],... 但是,这使得测试运行缓慢. 我现在只列出我需要的组件如下…… beforeEach(async(() => { // noinspection JSUnusedGlobalSymbols TestBed.configureTestingModule({ imports: [BrowserModule,FormsModule,HttpModule],// modules declarations: [ // pipes AttributeCheckPipe,// directives // DatePickerDirective,我有很多很多组件,我不确定要导入哪些组件.测试输出不告诉我需要导入哪些.它只是简单地传递(当我全部导入它们)或失败(如果我没有)但它没有告诉我需要哪些. 解决方法
最后在这里取得了一些进展我在compileComponents()中添加了一个catch块并记录了e.message并获得了一些有用的输出,这给了我一些工作!
继承我的代码.. beforeEach(async(() => { TestBed.configureTestingModule({ imports: [FormsModule,HttpModule,routing],// modules declarations: [ SaveSearchModalComponent ],providers: [ ESQueryService,RESTQueryService,] }).compileComponents() .then(() => { fix = TestBed.createComponent(SaveSearchModalComponent); instance = fix.componentInstance; injector = fix.debugElement.injector; }).catch((e) => { console.log(e.message); throw e; }); })); 错误消息输出的摘录是……
令人惊讶的是,文档中的任何地方都没有涉及(但我应该尽快猜到这一点!) 现在什么… 哇,在完成上述操作后(修复了99%的问题)我发现了另一个无用的错误信息…… 组件e不是任何NgModule的一部分,或者模块尚未导入模块. 哪个来自…… /node_modules/@angular/compiler/bundles/compiler.umd.js 所以按照…的建议 Angular 2 Component is not part of any NgModule 我将此日志语句添加到compiler.umd.js // I ADDED THIS LOG STATEMENT console.log('compType',String(compType)); // THIS LINE EXISTS ALREADY throw new Error("Component " + stringify(compType) + " is not part of any NgModule or the module has not been imported into your module."); 这通常是罪魁祸首.但是,在这里我得到了虚假输出……
提到这个.路人 所以我删除了路由导入,瞧! 但令人难以置信的是,这种痛苦是必要的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |