angular - Angular Karma 测试原型功能
问题描述
当我尝试运行 ng test 时,我收到一个错误
类型“FormArray”上不存在属性“myFunction”
它是一个添加到 app.module.ts 以使其全局化的属性。
它是扩展 AbstractControl 添加 myFunction 的原型,因此可以在 AbstractControl 对象上调用它。(完全像这里https://stackoverflow.com/a/47065916一样)
我可以做些什么来让 Karma 承认它被正确使用或让它忽略它?
编辑:完全错误:
app/modules/settings/components/some-folder/some-folder2/some-folder3/some-folder4/myComponent.component.ts:74:23 中的错误 - 错误 TS2339:属性 'myFunction' 在类型'FormArray 上不存在'。
74 myFormArray.addValidators([MyValidator(someList)]);
解决方案
当您运行测试时,app.module
不会隐式导入,因此扩展原型的代码不会被运行。
所以你要么
- 将整个导入到
app.module
您的 中TestBed.configureTestingModule
,这不是一个好习惯,因为它会引入所有依赖项, - 或创建一个测试模块,在其中运行原型扩展代码,并仅导入必要的依赖项(例如,表单的模块(包含
FormArray
),可能还有一些组件或服务) - 或者只是在 中运行一个原型扩展代码
beforeEach
,所以当你的测试到达有表单的部分时,原型已经被扩展了
beforeEach(() => {
// just paste the prototype extending code here, something like the following
AbstractControl.prototype.myFunction = () => {
// ...
}
// or even better, if you extracted the function above to somewhere
extendAbstractControl();
});`
推荐阅读
- transactions - Spring Cloud Stream Kafka 事务配置
- python - 有没有办法在 Tkinter 中确定窗口的优先级?
- angular - Mat Datepicker 在深色模式下没有拾取原色
- security - owasp 十大漏洞中的 ZAP 警报分类
- python - ModuleNotFoundError:没有名为“magenta.protobuf”的模块,但我已经安装了它
- reactjs - 关于react类组件setState的问题
- c++ - 使用 Range-v3 过滤 group_by 输出的正确方法是什么?
- python - 在 tensorflow 中查看错误的预测
- javascript - 不能在函数外使用变量。给出未定义的
- blockchain - 用于 getaccount/getbalance 的 TRC20 curl