Jus*_*ung 9 jasmine typescript
测试我的angular2应用程序.我尝试设置一个间谍,然后检查它被调用了多少次.我不断收到这个TS错误
类型'()=>任何'上不存在属性'调用'.
我该如何解决这个错误?
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);
});
});
Run Code Online (Sandbox Code Playgroud)
看起来SshRefService当前定义了getClient() : any. 结果,它正确地抛出了这个错误。发生这种情况是因为模拟过程用 Spy 替换了属性/方法,但 Typescript 无法知道发生了什么。
自从您监视了SshRefService.getClient,您有两种方法可以测试它是否被调用:
spyOn返回一个jasmine.Spy对象,该对象直接公开调用属性。您可以将结果保存在spyOn(ref, 'getClient').and.returnValue(true)示例对象上,然后像这样进行测试:
expect(getClientSpy.calls.count()).toEqual(1)
Run Code Online (Sandbox Code Playgroud)
首选(可能):您可以在对象本身的方法上运行 expect ,如下所示:
expect(ref.getClient).toHaveBeenCalledTimes(1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3390 次 |
| 最近记录: |