繁体   English   中英

Jasmine + TypeScript,找不到Spy类的withArgs()方法

[英]Jasmine + TypeScript, can`t find the withArgs () method of the Spy class

根据 Jasmine 文档,Spy 对象有一个方法 withArgs()

spyOn (someObj, 'func'). withArgs (1, 2, 3) .and.returnValue (42);

我在适用于 TypeScript 的版本中找不到这种方法。 我的项目是用 angular-cli(ng new) 创建的,Jasmine 是从盒子里提供的。 当我尝试调用 withArgs() 方法时,Visual Code 写信告诉我这个方法在 Spy 类中不存在......

您可能正在使用旧版本的 jasmine,或者旧版本的 jasmine 类型库。 这个特殊的方法是在Jasmine 3.0中引入的。 请注意,在Jasmine 2.9 文档中,该方法不存在。

您需要做的就是更新您的 Jasmine 和 jasmine 类型库。 假设您使用的是 npm,您可以执行以下操作:

npm i -D jasmine@latest jasmine-core@latest @types/jasmine@latest

这会将所有与 jasmine 相关的库更新为其最新版本并将其保存为 devDependencies。

就我而言,由于公司政策,在交付时间跨度内更新 Jasmine 库是不可能的。 因此我不能使用 withArgs。 我通过使用callFake解决了它并手动检查那里的参数。像这样:

mockAuthenticationService = jasmine.createSpyObj<AuthenticationService>('authenticationService', ['hasPermission']);
mockAuthenticationService.hasPermission.and.callFake((permission) => {
  return (permission === 'MyPermission');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM