【发布时间】:2020-04-26 07:11:00
【问题描述】:
我正在努力测试扩展另一个抽象类的组件类。
两个类如下:
export abstract class BaseListComponent {
constructor(
protected someImportantService: SomeImportantService
){
}
handleListInitialization() {
// Do lots of things
this.doOtherStuff();
}
/**
* @abstract doOtherStuff Function
*/
protected abstract doOtherStuff( );
}
export class MyListComponent extends BaseListComponent {
constructor(
someImportantService: SomeImportantService,
private listService: ListService
) {
super( someImportantService );
}
doStuff = () => {
this.handleListInitialization();
}
doOtherStuff(){
this.listService.getThings().then(() => {
// process response...
})
}
}
我正在尝试测试当在MyListComponent 中调用doStuff 时,它会导致在doOtherStuff 方法中调用listService.getThings()。
describe('When calling doStuff()', () => {
it('should call getThings from the ListService instance', ( ) => {
spyOn(component.listService, 'getThings').and.returnValue(Promise.then({foo: 'bar'}));
component.doStuff();
expect(component.listService.getThings).toHaveBeenCalled();
});
});
执行此测试时,我收到一条错误消息,指出从未调用过间谍,但奇怪的是,我的覆盖率报告显示我的 doOtherStuff() 实现的行已完全覆盖。
如果我在我的测试套件中调用doOtherStuff(),那么测试通过就好了。
我不明白为什么会发生这种情况,我想知道我的抽象基类是否以某种方式错误地实现了,尽管在运行应用程序时一切正常。
这可能是什么问题?
【问题讨论】: