首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角2业力测试‘成分-名称’不是一个已知的元素

角2业力测试‘成分-名称’不是一个已知的元素
EN

Stack Overflow用户
提问于 2017-06-12 16:34:55
回答 7查看 76.9K关注 0票数 155

在AppComponent中,我在HTML代码中使用nav组件。UI看起来很好。做服务时没有错误。当我看这个应用程序时,控制台上没有任何错误。

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

代码语言:javascript
复制
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

有:

代码语言:javascript
复制
import { NavComponent } from './nav/nav.component';

它也在NgModule的声明部分中。

代码语言:javascript
复制
@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

代码语言:javascript
复制
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

代码语言:javascript
复制
<app-nav></app-nav>
<div class="container-fluid">
</div>

我看到了一个类似的问题,但这个问题的答案是,我们应该在具有导出的nav组件中添加NgModule,但是当我这样做时,我会得到编译错误。

还有:app.component.spec.ts

代码语言:javascript
复制
import {NavComponent} from './nav/nav.component';
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2017-06-12 20:54:02

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

( A)在测试中声明原始NavComponent

代码语言:javascript
复制
describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          NavComponent
        ]
      }).compileComponents();
    }));

( B)伪装NavComponent

代码语言:javascript
复制
describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          MockNavComponent
        ]
      }).compileComponents();
    }));

// it(...) test cases 

});

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

您将在正式文件中找到更多信息。

票数 217
EN

Stack Overflow用户

发布于 2018-03-27 19:17:44

您也可以使用NO_ERRORS_SCHEMA

代码语言:javascript
复制
describe('AppComponent', () => {
beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [
      AppComponent
    ],
    schemas: [NO_ERRORS_SCHEMA]
  }).compileComponents();
}));

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

票数 9
EN

Stack Overflow用户

发布于 2019-11-04 10:08:54

对我来说,导入父程序中的组件解决了这个问题。

代码语言:javascript
复制
describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          NavComponent
        ]
      }).compileComponents();
    }));

在使用此组件的spec of the parent中添加此组件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44504468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档