类型'()=>上不存在如何解决“呼叫"的方法.任何'

编程入门 行业动态 更新时间:2024-10-23 21:26:08
本文介绍了类型'()=>上不存在如何解决“呼叫"的方法.任何'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

测试我的angular2应用.我尝试设置一个间谍,然后检查它被调用了多少次.我仍然不断收到此TS错误

Testing my angular2 app. I try and set a spy and then check how many times it has been called. I keep getting this TS error though

类型'()=> any'上不存在属性'calls'.

Property 'calls' does not exist on type '() => any'.

如何解决此错误?

How Do I resolve this error?

describe('ssh Service', () => { let ref:SshRefService; beforeEach(() => { TestBed.configureTestingModule({ providers: [ { provide: SshRefService, useClass: refClass }, ] }); }); beforeEach(inject([SshRefService], (sshRef:SshRefService) => { ref = sshRef spyOn(ref, 'getClient').and.returnValue(true) })); it('should mock an observable', () => { //service.list() calls ref.getClient() internally expect(service.list('/share')).toEqual(Observable.of(mockFileList)); expect(ref.getClient.calls.count()).toBe(1); }); });

推荐答案

看起来SshRefService当前定义了getClient() : any.结果,它正确地引发了此错误.之所以发生这种情况,是因为模拟过程用Spy替换了属性/方法,但是Typescript无法知道发生了什么.

It looks like SshRefService currently defines getClient() : any. As a result, it's correctly throwing this error. This is happening because the mocking process replaces the property/method with the Spy, but Typescript has no way of knowing that's taken place.

由于您监视了SshRefService.getClient,因此有两种方法可以测试它是否被调用:

Since you've spied on SshRefService.getClient , you have two ways to test whether it's been called:

spyOn返回一个jasmine.Spy对象,该对象直接公开calls属性.您可以将spyOn(ref, 'getClient').and.returnValue(true)的结果保存在示例对象上,然后像这样进行测试:

spyOn returns a jasmine.Spy object, which exposes the calls property directly. You can save the result of spyOn(ref, 'getClient').and.returnValue(true) on the example object, and then test that like so:

expect(getClientSpy.calls.count()).toEqual(1)

首选(可能):您可以在对象本身的方法上运行Expect,就像这样:

Preferred (probably): You can run expect on the method on the object itself, like so:

expect(ref.getClient).toHaveBeenCalledTimes(1)

更多推荐

类型'()=>上不存在如何解决“呼叫"的方法.任何'

本文发布于:2023-11-25 17:48:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630728.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   上不   类型   方法   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!