angular - ng-bootstrap Angular 配置 karma Jasmine

标签 angular jasmine karma-runner karma-jasmine ng-bootstrap

获取消息

If 'ngb-xx' is an Angular component, then verify that it is part of this module

对于我尝试的每个 angular bootstrap 组件

设置过程

npm install angular-cli
ng new project 
CD project
npm install 
npm install  --save bootstrap 
npm install --save @ng-bootstrap/ng-bootstrap

在 app.module.ts 中

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http' ;

import { NgbModule } from '@ng-bootstrap/ng-bootstrap' ;

import { AppComponent } from './app.component';
import { NgForm } from '@angular/forms/src/directives/ng_form';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

看起来一切都在使用 ng-bootstrap 和 angular

但是 karma 和 jasmine 不断抛出像

这样的错误

Failed: Template parse errors: 'ngb-tab' is not a known element:

  1. If 'ngb-tab' is an Angular component, then verify that it is part of this module.

  2. If 'ngb-tab' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

("

恩加勒特

Failed: Template parse errors: Can't bind to 'dismissible' since it isn't a known property of 'ngb-alert'.

  1. If 'ngb-alert' is an Angular component and it has 'dismissible' input, then verify that it is part of this module.

  2. If 'ngb-alert' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.

看起来我在 karma 和/或 jasmine 的配置中遗漏了一些东西

请帮忙 //Karma 配置文件,查看链接了解更多信息 //https://karma-runner.github.io/1.0/config/configuration-file.html

karma .conf.js

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

最佳答案

您的测试缺少 NgbModule 导入,您应该修复它

  • 导入模块

    import { NgbModule } from '@ng-bootstrap/ng-bootstrap' ;
    
  • 将其添加到 TestBed 导入

    TestBed.configureTestingModule({
        imports: [NgbModule, ...],
        declarations: [...],
        providers: [...]
    });
    

关于angular - ng-bootstrap Angular 配置 karma Jasmine ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518440/

相关文章:

angular - NO_ERRORS_SCHEMA 停止 Karma 测试以 Angular 工作

javascript - 需要帮助数据与 Observables 的 Angular 绑定(bind)

angular - 内部带有 *ngIf 的模板,更改模型后不起作用

javascript - Protractor + Simple-SSH 如何在 Jasmine 中同步

jquery - 这个 JavaScript 单元可以使用 Jasmine 进行测试吗?

angularjs - 如何使用 jasmine 测试 $window.open

css - 不是全局更改 ionic 搜索栏的占位符和清除图标颜色?

asp.net-mvc - 用于混合 cshtml 和 html 的 Webpack 配置(angular 2 + mvc)

Angular2 RC.1 - 将路由器注入(inject)单元测试

angularjs - 如何向 Ionic/Phonegap 项目添加测试?