gpt4 book ai didi

Angular 4 - 测试一个组件何时具有另一个组件

转载 作者:行者123 更新时间:2023-12-02 01:08:31 25 4
gpt4 key购买 nike

我正在对我的 Angular 应用程序进行测试:

ng test

这是我的 html 文件(page-not-found.component.html):

<menu></menu>
<div>
<h4>
ERROR 404 - PÁGINA NO ENCONTRADA
</h4>
</div>

当我运行 ng test 时,组件给我错误。

这是文件规范 (page-not-found.component.spec.ts)

import { MenuComponent } from '../menu/menu.component';

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

这是组件代码(pàge-not-found.component.ts)

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-page-not-found',
templateUrl: './page-not-found.component.html',
styleUrls: ['./page-not-found.component.css']
})
export class PageNotFoundComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}

这是错误:

Error: No provider for ActivatedRoute!

你能告诉我问题所在吗?谢谢

最佳答案

您需要在测试台中为 ActivatedRoute 提供一个提供程序。例如

 TestBed.configureTestingModule({
declarations: [ PageNotFoundComponent, MenuComponent ],
providers: [
{provide: ActivatedRoute, useValue: activatedRoute}, // using a mock
]
})

该菜单组件是否需要在您未找到的组件中?

关于Angular 4 - 测试一个组件何时具有另一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com