测试数组是否相等

时间:2018-07-17 09:28:46

标签: javascript unit-testing testing jasmine jestjs

在测试中如何比较数组的每个值,而不是整个数组? 在测试中,我比较了standardArray,但是我需要比较List [1,2,3,4],但我会得到它,以便测试不会失去其含义。 也许以某种方式通过索引或其他方式...

import { List, Set } from "immutable"
let standardArray = List([1,2,3,4]);

export function mass(standardArray) {
  let mutatedArray = standardArray.map(x => x * 2);
  return mutatedArray;
};

测试代码:

import { List, Set, isImmutable, Record, Map } from "immutable"
import { mass } from "./sum";


test('aligning arrays', () => {
  let standardArray = List([1,2,3,4]);
  for (let i = 0; i < 1; i++) {
   expect(mass(standardArray)).toEqual(standardArray.map(x => x * 2));
  };
});

1 个答案:

答案 0 :(得分:0)

如果要检查每个值,可以尝试如下操作:

test('values should match', () => {
  let originalArray = List([1,2,3,4]);
  let expectedResult = List([2,4,6,8]);
  let actuaResult = mass(originalArray);

  for (let i = 0; i < originalArray.length; i++) {
   expect(expectedResult[i]).toEqual(actuaResult[i]);
  };
});