angular - 用 karma jasmine angular2 测试 void
问题描述
我正在尝试用 jasmine 和 karma 测试我的代码。
当我测试一个返回值的方法时,没关系。但我的问题是如何测试一个 void 方法(什么都不返回),例如这个:
public aj(a: Array<x>, p: x) {
if (a.indexOf(p) < 0) {
a.push(p);
}
}
使用这个函数,我检查一个对象数组是否包含一个对象。
如果不是这种情况,我将其添加到数组中。就这样。
我这样测试
it('', () => {
let component= new synthese(consoService);
let x = [pHC,pHP]
spyOn(component,'aj');
expect(component.aj(x,pI)).toHaveBeenCalled();
});
我收到了这个错误
Error: <toHaveBeenCalled> : Expected a spy, but got undefined.
Usage: expect(<spyObj>).toHaveBeenCalled()
任何人都可以帮助我吗?我试过了,但总是出错。
解决方案
像这样更改您的代码:
it('', () => {
const component = new synthese(consoService);
const x = [pHC, pHP]; // maybe you should check this, shouldn't it be let x = ['pHC','pHP']; ?
component.aj(x, pI); // maybe you should check this, shouldn't it be component.aj(x, 'pI'); ?
// check pI is in the array now since that's what the method does, push the element if it is not in the array
expect(x).toContain(pl); // I used pI, but maybe check for 'pI' as my previous recommendations.
});
推荐阅读
- tags - Datadog 允许我创建构面,但它不显示它们的任何值
- android - 房间类型转换器地图
- flutter - Flutter - 隐藏系统栏的问题
- python - Python 3.8 嵌套变量不更新
- amazon-web-services - 在所有其他 lambda 完成后运行 lambda 函数
- javascript - 无法在 Express Js 中解析 POST 请求中的字符串
- swift - 如何在分段控制器内的 tableView 中保存选定行的状态?
- c# - MongoDB C# 驱动程序 2.13.1 对 System.ValueTuple 4.0.3.0 的间接依赖中断了 .Net 框架 4.8 的构建
- javascript - Mat Autocomplete 在 console.log Angular 中返回空字符串
- c# - 如何在 xamarin 表单中建模绑定嵌套视图