首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角8,茉莉花<spyOn>:找不到对象监视bind()

角8,茉莉花<spyOn>:找不到对象监视bind()
EN

Stack Overflow用户
提问于 2020-05-13 19:58:02
回答 1查看 823关注 0票数 1

对于角分量,我有一个基类,为了保持当前的上下文,我有以下绑定代码。如果我是console.log,我可以看到updatePreference被调用,但是茉莉无法检测它是否被调用。

如果我使用以下命令,标题会出现错误。

代码语言:javascript
复制
spyOn(fixture.componentInstance.updatePreference.prototype, 'bind').and.callThrough();

我的装置上有遗漏什么吗?

配置文件-组件.扩展配置文件-base.ts

代码语言:javascript
复制
public ngOnInit(): void {
    this.updatePreference = this.updatePreference.bind(this);
}

 public openRemovePanel(itemGuid: string) {
    super.openRemovePanel(itemGuid, this.updatePreference);
  }

概要文件-base.ts(基类)

代码语言:javascript
复制
public openRemovePanel(itemGuid: string, callbackFunction: (id: string, data: string) => void) { 
….
callbackFunction(id, data);
}

Profile-component.spec.ts

代码语言:javascript
复制
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();
})
EN

回答 1

Stack Overflow用户

发布于 2020-05-13 21:13:10

试过几次失败后,我注意到,如果我按下面的顺序重新安排订单的话,它是有效的。

代码语言:javascript
复制
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');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61783731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档