gpt4 book ai didi

angular - 使用@ViewChild 对 Angular 5 组件进行单元测试

转载 作者:太空狗 更新时间:2023-10-29 17:23:02 29 4
gpt4 key购买 nike

我使用的是 angular 5.2.0。我有一个子组件

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

@Component({
template: `<div><div></div></div>`,
})
export class ChildComponent {

public childMethod() {
...
}
}

和一个通过 ViewChild 访问子组件的父组件

import { Component, ViewChild } from "@angular/core";
import { ChildComponent } from "child.component";

@Component({
template: `
<child-component #child>
<child-component></child-component>
</child-component>
`,
})
export class ParentComponent {
@ViewChild("child") child: ChildComponent;

public parentMethod() {
this.child.childMethod();
}
}

我想要一个单元测试来证明 parentMethod 的调用导致调用 childMethod .我有以下内容:


import { NO_ERRORS_SCHEMA } from "@angular/core";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { ChildComponent } from "./child.component";
import { ParentComponent } from "./parent.component";

describe("ParentComponent", () => {
let component: Parentcomponent;
let fixture: ComponentFixture<Parentcomponent>;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ParentComponent, ChildComponent],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TaskListPaginatorComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it("should invoke childMethod when parentMethod is invoked", () => {
const childMethodSpy: jasmine.Spy = spyOn(component.child, "childMethod");
component.parentMethod();
expect(childMethodSpy).toHaveBeenCalled();
});
});

然而,这不起作用,我得到 Error: <spyOn> : could not find an object to spy upon for childMethod() .

此外,这不是单元测试,因为我使用的是真正的 ChildComponent 而不是模拟。我尝试创建一个 MockChildComponent 并将其添加到 declarationsexport但我得到了相同的结果。有帮助吗?

我知道有类似的帖子,但它们针对的是不同版本的 angular,但没有帮助。

最佳答案

你可以这样做。

像这样为 ChildComponent 创建一个 spy 对象。

const childComponent = jasmine.createSpyObj('ChildComponent', ['childMethod']);

然后在测试中,将组件的 childComponent 属性设置为您创建的 spy 。

component.childComponent =  childComponent;

您的测试文件应如下所示。

import { NO_ERRORS_SCHEMA } from "@angular/core";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { ChildComponent } from "./child.component";
import { ParentComponent } from "./parent.component";

describe("ParentComponent", () => {
let component: ParentComponent;
let fixture: ComponentFixture<ParentComponent>;

const childComponent = jasmine.createSpyObj("ChildComponent", [
"childMethod",
]);

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ParentComponent, ChildComponent],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(ParentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it("should invoke childMethod when parentMethod is invoked", () => {
component.childComponent = childComponent;
component.parentMethod();
expect(childComponent.childMethod).toHaveBeenCalled();
});
});


关于angular - 使用@ViewChild 对 Angular 5 组件进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51515530/

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