angular - Angular Karma/Jesmine spyOn 创建类型“x”不能分配给“从不”类型的参数
问题描述
我在下面有一个常量类。当我尝试使用 spyOn 返回不同的值以测试错误情况时,ng test 返回编译时错误类型的参数'"retryCount"' is not assignable to parameter of type 'never'.
有没有办法解决这个问题?
export class NConstants {
public static retryCount: number = 2;
public static readonly retryDelayInMilliseconds: number = 10000; // ms
public static readonly retryNotificationDelayInSeconds: number = 50;
}
spyOn(NetworkConstants, 'retryCount').and.returnValue(0);
所有套餐
"jasmine-auto-spies": "^4.1.0",
"jasmine-core": "~3.3.0",
"jasmine-marbles": "^0.6.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "^3.1.3",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "^2.0.0",
"karma-coverage-istanbul-reporter": "^2.0.4",
"karma-firefox-launcher": "^1.1.0",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"karma-trx-reporter": "^0.3.0",
解决方案
您只能spyOn
使用类的方法/函数,而不是实例变量。
要改成返回0,直接修改即可。
NConstants.retryCount = 0;
// do what you want.
// further down the line, you can reset it
NConstants.retryCounter = 2;
推荐阅读
- python - 如何使用 Selenium + Python 向下滚动对话框
- typescript - 如何理解打字稿中的“属性'名称'在'用户'类型中是私有的”
- flutter - 颤振 sqflite 错误:flutterEngine.getPlugins().add(new com.tekartik.sqflite.SqflitePlugin());
- android - 如何计算开始日期和结束日期的差异
- r - 将长数据除以R中另一个数据集中的值
- php - 完全加载后如何获取网站内容
- java - 如何在 IntelliJ 中使用我的 maven 用户 settings.xml?
- xcode - 使用 xcodebuild 构建模拟器应用程序版本时找不到“Cordova/CDV.h”文件
- r - 在函数中将数据框的名称转换为字符串
- c# - 在c#中使用固定的第一个元素和第二个变量元素拆分字符串