Angular执行测试报错: 'app-messages' is not a known element

Angular执行测试报错: ‘app-messages’ is not a known element

本机环境:

$ ng --version
Angular CLI: 7.1.1
Node: 10.11.0
OS: darwin x64
Angular: 7.1.1

错误详情:

Chrome 72.0.3622 (Mac OS X 10.13.6) AppComponent should create the app FAILED
	'app-messages' is not a known element:
	1. If 'app-messages' is an Angular component, then verify that it is part of this module.
	2. If 'app-messages' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
	<router-outlet></router-outlet>
	[ERROR ->]<app-messages></app-messages>"): ng:///DynamicTestModule/AppComponent.html@22:0

问题分析:

使用 ng serve 运行应用,通过访问 http://localhost:4200,都运行正常,未见报错。但是,执行 ng test 就提示一大推错误,这是为什么呢?

提示这个“不是已知元素”,一般是没有引入组件,可是在代码里已经都引入过了,为何还会这样报错呢?

最后发现,在应用代码里需要引入外,在测试代码(*.spec.ts)里也要再次引入相应的组件或模块。

比如上边例子中,在AppComponent组件中,使用了 app-messages 组件模板,但是并没有引入 app-messages 组件(MessagesComponent)。

解决办法:

引入相应的组件。

类似XXX is not a known element的错误还有很多,引入相应的组件即可。

比如,在 DashboardComponent 组件中,使用了 app-hero-search 组件模板,却没有引入 HeroSearchComponent 组件:

Chrome 72.0.3622 (Mac OS X 10.13.6) DashboardComponent should create FAILED
	'app-hero-search' is not a known element:
	1. If 'app-hero-search' is an Angular component, then verify that it is part of this module.
	2. If 'app-hero-search' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
	</div>
	[ERROR ->]<app-hero-search></app-hero-search>
	"): ng:///DynamicTestModule/DashboardComponent.html@12:0

修复后代码:

修改后 dashboard.component.spec.tsbeforeEach 部分代码如下:

// dashboard.component.spec.ts 
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ 
        DashboardComponent, 
        MessagesComponent, 
        HeroSearchComponent
      ],
      imports:[
        RouterTestingModule,
        HttpClientTestingModule
      ]
    })
    .compileComponents();
  }));

更多内容,请查看项目代码:

https://github.com/cnwyt/angular-tour-of-heroes

[END]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web后端技术

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值