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

Evg*_*nko 5 javascript jasmine typescript angular

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

spyOn (someObj, 'func'). withArgs (1, 2, 3) .and.returnValue (42);
Run Code Online (Sandbox Code Playgroud)

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

And*_*erg 5

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

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

npm i -D jasmine@latest jasmine-core@latest @types/jasmine@latest
Run Code Online (Sandbox Code Playgroud)

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