加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单元测试 – 如何知道单元测试时要导入哪些组件?

发布时间:2020-12-17 17:27:19 所属栏目:安全 来源:网络整理
导读:我正在使用Angular2(2.1.0)最终版本. 我在使用单元测试时通过AppModule导入所有组件… beforeEach(async(() = { TestBed.configureTestingModule({ imports: [AppModule],... 但是,这使得测试运行缓慢. 我现在只列出我需要的组件如下…… beforeEach(async((
我正在使用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,我有很多很多组件,我不确定要导入哪些组件.测试输出不告诉我需要导入哪些.它只是简单地传递(当我全部导入它们)或失败(如果我没有)但它没有告诉我需要哪些.

这个错误很烦人/无用..

invokeTask@node_modules/zone.js/dist/zone.min.js:1:36996
onInvokeTask@node_modules/zone.js/dist/proxy.min.js:1:2190
invokeTask@node_modules/zone.js/dist/zone.min.js:1:36939
runTask@node_modules/zone.js/dist/zone.min.js:1:31466
a@node_modules/zone.js/dist/zone.min.js:1:17818
g@node_modules/core-js/client/shim.min.js:8:19058
node_modules/core-js/client/shim.min.js:8:19180
k@node_modules/core-js/client/shim.min.js:8:14294
l@node_modules/zone.js/dist/zone.min.js:1:18418
l@node_modules/zone.js/dist/zone.min.js:1:18175
node_modules/zone.js/dist/zone.min.js:1:18715

如何获得有关我无法导入哪些组件的反馈?谢谢

我正在使用Karma和PhantomJS.

我的Karma配置摘录是..

client: {
  captureConsole: true
},logLevel: config.LOG_DEBUG

解决方法

最后在这里取得了一些进展我在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;
    });
  }));

错误消息输出的摘录是……

‘dynamic-form’ is not a known element:
1. If ‘dynamic-form’ is an Angular component,then verify that it is part of this module.
2. If ‘dynamic-form’ is a Web Component then add “CUSTOM_ELEMENTS_SCHEMA” to the ‘@NgModule.schemas’ of this component
to suppress this message.

令人惊讶的是,文档中的任何地方都没有涉及(但我应该尽快猜到这一点!)

现在什么…

哇,在完成上述操作后(修复了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.");

这通常是罪魁祸首.但是,在这里我得到了虚假输出……

LOG: ‘function e(e)
{__cov_m4LFTxiG42jWqZk7He0hiA.f[‘4′]++;__cov_m4LFTxiG42jWqZk7He0hiA.s[’11’]++;this.router=e,this.formErrors={invalidCreds:!1};}’

提到这个.路人

所以我删除了路由导入,瞧!

但令人难以置信的是,这种痛苦是必要的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读