angular - 茉莉花:由于间谍无法测试方法
angular - jasmine : Cant test method because of spy
我的组件中有一些具体方法:
public show(summary: GridSummary) {
this.resetModal(summary);
this.summary.direction = this.summary.direction || 'Response';
this.title = this.getTitle(summary);
this.parentId = this.summary.id;
this.parentType = 'Dcc' + this.summary.direction;
this.openModal(this.dialog);
this.auditService.getAudits(this.summary.pipelineId).subscribe(
(summaries) => { this.populateTabs(summaries); },
(error) => this.showSubmitError(error));
}
public hasSummaries(): boolean {
return this.auditSummaries && this.auditSummaries.length > 0;
}
private populateTabs(summaries: AuditSummary[]) {
this.auditSummaries = summaries;
if (this.isFailed()) {
if (this.isDtcFlow() || this.isDataExport()) {
this.getErrorDetails();
} else {
this.getComments(false);
}
} else {
if (this.isRequest()) {
this.getComments(false);
}
this.dataReady = true;
}
}
auditSummaries 通过 show 方法设置。
所以在我的规范文件中,我在我的 show 方法上放置了一个间谍。
设置:
showSpy = spyOn(comp, 'show');
在我的每一个之前。然后在我的 'it' 测试中:
it('Show shows the model', async(() => {
fixture.detectChanges();
expect(showSpy).toHaveBeenCalled();
}));
这个测试没问题。我遇到的问题是尝试测试 hasSummaries 是否已设置为:
it('hasSummaries return true after show', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(comp.hasSummaries()).toBe(true);
});
}));
因为我在主要方法上使用了一个间谍,所以审计服务并没有真正被调用。所以我无法以上述方式测试 hasSummaries。我该如何测试这个。
我正在使用 Angular 9、Jasmine 3.5、Karma 4.4
如果你想监视一个函数并保留原来的行为,你需要使用callThrough。
来自文档:
By chaining the spy with and.callThrough, the spy will still track all
calls to it but in addition it will delegate to the actual
implementation.
在你的情况下,你会想要做:
spyOn(comp, 'show').and.callThrough()
我的组件中有一些具体方法:
public show(summary: GridSummary) {
this.resetModal(summary);
this.summary.direction = this.summary.direction || 'Response';
this.title = this.getTitle(summary);
this.parentId = this.summary.id;
this.parentType = 'Dcc' + this.summary.direction;
this.openModal(this.dialog);
this.auditService.getAudits(this.summary.pipelineId).subscribe(
(summaries) => { this.populateTabs(summaries); },
(error) => this.showSubmitError(error));
}
public hasSummaries(): boolean {
return this.auditSummaries && this.auditSummaries.length > 0;
}
private populateTabs(summaries: AuditSummary[]) {
this.auditSummaries = summaries;
if (this.isFailed()) {
if (this.isDtcFlow() || this.isDataExport()) {
this.getErrorDetails();
} else {
this.getComments(false);
}
} else {
if (this.isRequest()) {
this.getComments(false);
}
this.dataReady = true;
}
}
auditSummaries 通过 show 方法设置。
所以在我的规范文件中,我在我的 show 方法上放置了一个间谍。
设置:
showSpy = spyOn(comp, 'show');
在我的每一个之前。然后在我的 'it' 测试中:
it('Show shows the model', async(() => {
fixture.detectChanges();
expect(showSpy).toHaveBeenCalled();
}));
这个测试没问题。我遇到的问题是尝试测试 hasSummaries 是否已设置为:
it('hasSummaries return true after show', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(comp.hasSummaries()).toBe(true);
});
}));
因为我在主要方法上使用了一个间谍,所以审计服务并没有真正被调用。所以我无法以上述方式测试 hasSummaries。我该如何测试这个。
我正在使用 Angular 9、Jasmine 3.5、Karma 4.4
如果你想监视一个函数并保留原来的行为,你需要使用callThrough。
来自文档:
By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.
在你的情况下,你会想要做:
spyOn(comp, 'show').and.callThrough()