期望一个浮点数数组接近Jasmine中的另一个数组 [英] Expect an array of float numbers to be close to another array in Jasmine
问题描述
我正在测试一个返回数组数组的Javascript函数,以查看返回的数组是否包含与包含期望输出的数组相同的元素:
期望(myArray的).toEqual(expectedArray);
如果myArray和expectedArray只包含整数,但是如果至少存在一个浮点,由于浮点精度错误。 toBeCloseTo
在数组上似乎不起作用。
目前我正在做一个循环来执行成员检查:
$ $ $ $
expect(myArray [I])toBeCloseTo(expectedArray [I])。
}
...但是有更简单的方法吗?如果测试因为某种原因失败,那么输出会被一个可疑的错误消息所膨胀。 下面的代码应该回答你的问题:
actual.every((x,i)=> expect(x) .toBeCloseTo(预期[I]));
I'm testing a Javascript function returning an array of numbers, to see if the returned array contains the same elements as the array containing the expected output:
expect(myArray).toEqual(expectedArray);
This works flawlessly if myArray and expectedArray only contain integers, but fail if there is at least one float present, due to floating-point precision errors. toBeCloseTo
does not seem to function on arrays.
Currently I'm doing a loop to to do member-wise checking:
for (var i = 0; i < myArray.length; i++) {
expect(myArray[i]).toBeCloseTo(expectedArray[i]);
}
... but is there a cleaner way to do this? If the test fails for whatever reason, the output is bloated with a hideous amount of error messages.
The following code should answer your question:
actual.every((x, i) => expect(x).toBeCloseTo(expected[i]));
这篇关于期望一个浮点数数组接近Jasmine中的另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!