对于角分量,我有一个基类,为了保持当前的上下文,我有以下绑定代码。如果我是console.log,我可以看到updatePreference被调用,但是茉莉无法检测它是否被调用。
如果我使用以下命令,标题会出现错误。
spyOn(fixture.componentInstance.updatePreference.prototype, 'bind').and.callThrough();我的装置上有遗漏什么吗?
配置文件-组件.扩展配置文件-base.ts
public ngOnInit(): void {
this.updatePreference = this.updatePreference.bind(this);
}
public openRemovePanel(itemGuid: string) {
super.openRemovePanel(itemGuid, this.updatePreference);
}概要文件-base.ts(基类)
public openRemovePanel(itemGuid: string, callbackFunction: (id: string, data: string) => void) {
….
callbackFunction(id, data);
}Profile-component.spec.ts
fit('this should work', () => {
let fixture = TestBed.createComponent(ProfileComponent);
fixture.detectChanges();
// tried both of the following
spyOn(fixture.componentInstance.updatePreference.prototype, 'bind').and.callThrough();
spyOn(fixture.componentInstance, updatePreference).and.callThrough();
…...
// make a call to openRemovePanel, and that triggers updatePreference
……
// this does not work
expect(fixture.componentInstance.updatePreference).toHaveBeenCalled();
})发布于 2020-05-13 21:13:10
试过几次失败后,我注意到,如果我按下面的顺序重新安排订单的话,它是有效的。
let fixture = TestBed.createComponent(ProfileComponent);
// hold the spy reference
let spyReference = spyOn(fixture.componentInstance, updatePreference).and.callThrough();
// then trigger OnInit to do binding
fixture.detectChanges();
// then write your expection against spy reference
expect(spyReference ).toHaveBeenCalledWith('123', '456');https://stackoverflow.com/questions/61783731
复制相似问题