Angular 2 Karma Test'component-name'不是已知元素

问题描述 投票:70回答:3

在AppComponent中,我在HTML代码中使用nav组件。用户界面看起来很好。做服务时没有错误。当我查看应用程序时,控制台中没有错误。

但是当我为我的项目运行Karma时,出现了一个错误:

Failed: Template parse errors: 
'app-nav' is not a known element:
1. If 'app-nav' is an Angular component, then verify that it is part of this module.
2. If 'app-nav' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

在我的app.module.ts中:

有:

import { NavComponent } from './nav/nav.component';

它也在NgModule的声明部分

@NgModule({
  declarations: [
    AppComponent,
    CafeComponent,
    ModalComponent,
    NavComponent,
    NewsFeedComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    JsonpModule,
    ModalModule.forRoot(),
    ModalModule,
    NgbModule.forRoot(),
    BootstrapModalModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

我在我的NavComponent中使用AppComponent

app.component.ts

import { Component, ViewContainerRef } from '@angular/core';
import { Overlay } from 'angular2-modal';
import { Modal } from 'angular2-modal/plugins/bootstrap';
import { NavComponent } from './nav/nav.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Angela';
}

app.component.html

<app-nav></app-nav>
<div class="container-fluid">
</div>

我已经看到了一个类似的问题,但是那个问题的答案是我们应该在导航组件中添加NgModule,其中有导出,但是当我这样做时我遇到了编译错误。

还有:app.component.spec.ts

import {NavComponent} from './nav/nav.component';
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
javascript angular typescript karma-jasmine
3个回答
114
投票

因为在单元测试中,您希望测试主要与应用程序的其他部分隔离的组件,所以Angular默认情况下不会添加模块的依赖关系,如组件,服务等。所以你需要在测试中手动完成。基本上,这里有两个选项:

A) Declare the original NavComponent in the test

describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          NavComponent
        ]
      }).compileComponents();
    }));

B) Mock the NavComponent

describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          MockNavComponent
        ]
      }).compileComponents();
    }));

// it(...) test cases 

});

@Component({
  selector: 'app-nav',
  template: ''
})
class MockNavComponent {
}

你会在official documentation找到更多信息。


5
投票

你也可以使用NO_ERRORS_SCHEMA

describe('AppComponent', () => {
beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [
      AppComponent
    ],
    schemas: [NO_ERRORS_SCHEMA]
  }).compileComponents();
}));

https://www.ng-conf.org/mocking-dependencies-angular/


0
投票

还有一个原因是,在你的测试案例中,.compileComponents()可以有多个beforeEach()

例如

beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [TestComponent] }).compileComponents(); }));

beforeEach(() => { TestBed.configureTestingModule({ imports: [HttpClientModule], declarations: [Test1Component], providers: [HttpErrorHandlerService] }).compileComponents(); });

© www.soinside.com 2019 - 2024. All rights reserved.