angular - Angular Jasmin ObjectSpy 不是函数
问题描述
每当在任何测试中调用服务时,我都会尝试监视服务,因此创建一个模拟:
import { SpyObject } from './spyobject';
import { TranslateService } from '@ngx-translate/core';
export class MockTranslateService extends SpyObject {
constructor() {
super(TranslateService);
this.spy('instant').andReturn('');
}
}
我这样提供:
{
provide: TranslateService,
useValue: MockTranslateService,
},
我的组件使用this.translateService.instant
并获得:TypeError: this.translateService.instant is not a function
.
另一方面,当我在组件中记录 translateService 时,我得到:
class MockTranslateService extends spyobject_1.SpyObject {
constructor() {
super(core_1.TranslateService);
this.spy('instant').andReturn(this);
}
}
知道为什么间谍不工作吗?提前致谢。
解决方案
为了this.spy('instant')
工作,您需要一个监视功能。这将起作用:
export class MockTranslateService extends SpyObject {
constructor() {
super(TranslateService);
this.spy('instant').andReturn('');
}
public instant() { }
}
由于该instant
函数已定义,因此可以添加间谍。
编辑这可能更容易。在一个beforeEach
块中,执行以下操作:
beforeEach(() => {
spyOn(TranslateService.prototype, 'instant');
});
这会将间谍直接放在原型上,并在每次测试后对其进行清理。
推荐阅读
- postgresql - 在 Postgres DB 上的 PLPGSQL 过程中使用 BEGIN/END 事务管理时的方法/逻辑?
- python - 如何更改 networkx 图中的节点大小?
- android - 离子离子按钮颜色属性在android中不起作用
- r - 根据每次按下任何 actionButton() 在闪亮的应用程序中显示不同的图像对
- c# - 为什么它仍然访问状态被删除的行并得到错误
- c# - WPF MultiBinding 和 IMultiValueConverter 进行布尔运算
- class - UML 中对象的类可以改变吗?
- ocaml - 什么| ocaml中的符号是什么意思?
- python - 使用 BeautifulSoup提取包含 `
` 的字符串返回 `None` - r - 在 R 中重复复制()以逐步向前/向后更改(每次更改种子)