of-bootstrap angular 配置业力茉莉花

ng-bootstrap angular config karma jasmine

收到消息

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.

("

ngalert

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 配置中遗漏了一些东西 and/or jasmine

请帮忙 // Karma 配置文件,参见 link 获取更多信息 // 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: [...]
    });