为相等的数组编写正确的断言测试

时间:2018-09-22 07:27:30

标签: javascript arrays assertion

我在编写正确的断言测试时遇到麻烦。它在单个数组上工作正常,但是在测试数组数组时不起作用。问题似乎出在myMap["key"] = 0.25 myMap["key2"] = "some string" 上,但我不知道为什么。我在做什么错了?

areEqualItems

6 个答案:

答案 0 :(得分:2)

您可以创建一个非常简洁的equals函数。只需检查长度不相等或不是数组的边缘情况,然后递归:

var arr3 = [[0,1,2,3], [0,1,2,3]];
var arr4 = [[0,1,2,3], [0,1,2,3]];


function equal(a, b){
  if (!Array.isArray(a) || !Array.isArray(b)) return a === b
  if (a.length !== b.length) return false

  return a.every((item, i) => equal(item, b[i]))
}

console.log(equal(arr3, arr4))
console.log(equal([[1, 2], [3, 4]], [[2, 2], [3, 4]]))

答案 1 :(得分:1)

关于如何使其递归有一些帮助:

function areArraySame(one, two) {
    var length = one.length === two.length;
    var allElems = one.every(function(item, i) {
        if (item instanceof Array) {
            return areArraySame(item, two[i]); // <-- recurse!
        }
        return item === two[i];
    });

    return length && allElems;
}

答案 2 :(得分:1)

如果元素是另一个数组,则需要使其递归。

此外,当长度不匹配时,检查项目是否相等也没有意义。

如果您从比较功能中退出日志记录也会更好。我必须以是否提供testName参数为条件,以便在所有递归调用期间都不会记录该参数。

function assertArraysEqual(actual, expected, testName) {
  var areEqualLength = actual.length === expected.length;
  var areEqualItems = areEqualLength && actual.every(function(item, i) {

    if (Array.isArray(item) && Array.isArray(expected[i])) {
      return assertArraysEqual(item, expected[i]);
    } else {
      return item === expected[i]
    }
  });
  if (testName) {
    if (areEqualItems) {
      console.log('passed');
    } else {
      console.log('FAILED [' + testName + '] Expected ' + expected + ', but got ' + actual);
    }
  }
  return areEqualItems;
}

var arr1 = [0, 1, 2, 3];
var arr2 = [0, 1, 2, 3];

var arr3 = [
  [0, 1, 2, 3],
  [0, 1, 2, 3]
];
var arr4 = [
  [0, 1, 2, 3],
  [0, 1, 2, 3]
];
var arr5 = [
  [0, 1, 2, 3],
  [0, 2, 3, 4]
];

assertArraysEqual(arr1, arr2, 'arrays should be equal'); // passed
assertArraysEqual(arr3, arr4, 'arrays should be equal'); // FALIED [arrays should be equal] Expected 0,1,2,3,0,1,2,3, but got 0,1,2,3,0,1,2,3
assertArraysEqual(arr3, arr5, 'arrays should not be equal');

答案 3 :(得分:1)

为什么不对数组进行字符串化然后比较?它将大大减少您的代码。

function assertArraysEqual(actual, expected) {
    const actualStr = JSON.stringify(actual);
    const expectedStr = JSON.stringify(expected);
    return actualStr === expectedStr;
}

答案 4 :(得分:0)

将这项工作交给第三方的另一种解决方案,例如lodash。

Lodash具有isEqual方法,可以为您完成工作。

查看文档here

示例代码:

function assertArraysEqual(actual, expected, testName) {
    if (_.isEqual(actual, expected)) {
        console.log('passed');
    } else {
        console.log(`FALIED [${testName}] Expected ${expected}, but got ${actual}`);
    }
}

答案 5 :(得分:0)

比较数组(及其子数组及其子数组)可能很快变得非常昂贵。此外,您编写的代码以及可接受的答案均不考虑关联数组(即对象)。我建议将两个数组都转换为JSON字符串,然后将它们作为字符串进行比较。

function assertArraysEqual(actual, expected, testName){
    if ( actual.length === expected.length && JSON.stringify(actual) == JSON.stringify(expected) ) console.log('passed');
    else console.log('FALIED [' + testName + '] Expected ' + expected + ', but got ' + actual);
}

var arr1 = [0,1,2,3];
var arr2 = [0,1,2,3];

var arr3 = [ [0,1,2,3], [0,1,2,3] ];
var arr4 = [ [0,1,2,3], [0,1,2,3] ];

assertArraysEqual(arr1, arr2, 'arrays should be equal'); 
assertArraysEqual(arr3, arr4, 'arrays should be equal');