【发布时间】:2021-12-18 16:07:09
【问题描述】:
我尝试测试我的组件,我知道该组件工作正常,但我的测试出错,因为 Angular 版本已更新到 12。
这是我的组件:
ngOnInit() {
if (versonA) {
this.doThis();
} else {
this.doThat();
}
}
private doThis() {
this.myService.confirm({
message: message,
accept: () => {
this.doAcceptLogic();
}, reject: () => {
console.log('reject')
this.doRejectLogic();
}
});
}
这是我的测试:
beforeEach(async () => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.autoDetectChanges();
spyOn(TestBed.get(MyService), 'confirm').and.callFake((params: Confirmation) => {
params.reject();
});
await fixture.whenStable();
});
而且,这个 spyOn 似乎仍然不起作用。 我将很多控制台日志放入我的代码中,doThis() 方法仍然被调用,但我的确认方法('reject')中的日志没有被写入控制台。 我不明白为什么。
当我将 doThis() 方法更改为 public 并从我的测试中直接调用它为 component.doThis() 时,它会运行到模拟的 myService。
谁能解释我的原因?
非常感谢!
【问题讨论】:
标签: javascript angular testing karma-jasmine spyon