[英]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.