【问题标题】:Ionic 2 karma test failed 'ion-label' is not a known elementIonic 2 业力测试失败“离子标签”不是已知元素
【发布时间】:2017-11-06 06:17:42
【问题描述】:

在运行 npm test 时,我的组件出现此错误 ->

  **Can't bind to 'formGroup' since it isn't a known property of 
   'form'. 
   ("
  </div>
<form [ERROR ->][formGroup]="loginForm" text-center>
<ion-list>
"): ng:///DynamicTestModule/Login.html@18:6
'ion-label' is not a known element:
1. If 'ion-label' is an Angular component, then verify that it is part 
of this module.
2. If 'ion-label' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' 
to the '@NgModule.schemas' of this component to suppress this message. 
 ("
<ion-item>
    [ERROR ->]<ion-label floating>Email</ion-label>
    <ion-input type="email" formControlName="email" [class.inva"): 
 ng:///DynamicTestModule/Login.html@25:4
'ion-input' is not a known element:
1. If 'ion-input' is an Angular component, then verify that it is part 
of this module.
2. If 'ion-input' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<ion-item>
    <ion-label floating>Email</ion-label>
    [ERROR ->]<ion-input type="email" formControlName="email" [class.invalid]="!loginForm.controls.email.valid && ("): ng:///DynamicTestModule/Login.html@26:4
'ion-item' is not a known element:

**1。如果 'ion-item' 是一个 Angular 组件,那么验证它是这个模块的一部分。 2. 如果 'ion-item' 是一个 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到此组件的 '@NgModule.schemas' enter code here 以禁止显示此消息。 (" [错误->]****

这是我的 test.ts

     import 'zone.js/dist/long-stack-trace-zone';
      import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { getTestBed, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { App, Config, Form, IonicModule, Keyboard, DomController, MenuController, NavController, Platform } from 'ionic-angular';
import { ConfigMock } from './mocks';




// Unfortunately there's no typing for the `__karma__` variable. Just 
 declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function (): void {
  // noop
};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
);

// Then we find all the tests.
let context: any = require.context('./', true, /\.spec\.ts/);

// And load the modules.
context.keys().map(context);

// Finally, start Karma to run the tests.
__karma__.start();
``

【问题讨论】:

标签: angular unit-testing ionic-framework ionic2 karma-jasmine


【解决方案1】:

您在配置测试套件时是否导入了 IonicModule? 类似于(在 test.ts 中):

public static configureIonicTestingModule(components: Array<any>): typeof TestBed {
  return TestBed.configureTestingModule({
    declarations: [
      ...components,
    ],
    providers: [
      App, Form, DomController
    ],
    imports: [
      FormsModule,
      IonicModule, // <- this here
    ],
  });
}

【讨论】:

  • 我不明白应该在哪里添加这是我的 test.ts
  • @sachinkaushik 你的测试可能有一个在 karma.conf.ts 中定义的测试入口点,通常是 src/test.ts。您应该将此添加到 karma.conf.js 中定义的 test.ts
  • 我添加了仍然面临同样的问题,但现在我收到此错误:无法解析 FormGroup 的所有参数:(?,?,?)。
  • @sachinkaushik 我想你将它添加到你的 .spec.ts 文件中
猜你喜欢
  • 1970-01-01
  • 2017-06-04
  • 2020-06-10
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多