Angular e2e-应该在h1标签中呈现标题

时间:2018-12-18 05:38:45

标签: angular karma-jasmine angular-e2e

我正在开发自己的网站。我是Angular的新手,只是基于学习和在线帖子,我开始开发自己的网站。在运行测试时,我在获取h1标签值时遇到错误。

我开发了路线。 AppComponent加载的路由之一是其中具有h1标签的HomeComponent。

在app.component.spec.ts文件中,以下无法获取h1的值。

it('should render title in a h1 tag', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome to jc-web!');});

有人可以建议我如何从上述代码中的HomeComponent中获取h1值吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

要访问嵌套在AppComponent中的HomeComponent中的h1值,您需要在TestBed中提供实际的HomeComponent:

TestBed.configureTestingModule({
    declarations: [
        AppComponent,
        HomeComponent
    ]
});

或提供存根版本供您的测试使用。如果选择提供实际的组件,则还必须包括其所有依赖项。在Angular文档here中有关于如何测试嵌套组件的详细说明。