【发布时间】:2023-03-29 16:53:01
【问题描述】:
我正在使用 Karma 为 Angular 2 编写单元测试用例,我遇到了一个函数,我在其中运行测试以进行下一行
expect(component.subscribeToEvents()).toBeTruthy();
我查看了我的覆盖率代码,测试文件中的行似乎没有覆盖订阅中的任何内容。我曾尝试使用 MockBackend 在服务函数中模拟 api 调用,但我不确定如何对订阅的对象进行模拟,有人可以帮我吗?
下面是test.component.ts
subscribeToEvents() {
this.subscription = this.czData.$selectedColorZone
.subscribe(items => {
this.resourceLoading = true;
if (!this.resourceData || (this.resourceData && this.resourceData.length === 0)) {
this.settings.layout.flypanel.display = false;
this.getAllResources(this.pagination.start, this.pagination.size);
}
else {
this.pagination.start = 1;
this.pagination.end = this.pagination.size;
this.getAllResources(1, this.pagination.size);
this.settings.layout.flypanel.display = true;
}
});
return true;
}
【问题讨论】:
标签: unit-testing angular karma-jasmine angular2-services karma-coverage