assertEquals

时间:2017-09-18 08:36:24

标签: javascript loops tdd equality

我试图比较2个对象,首先是它们是否具有相同的键,然后通过它们的键值相等,但来自assertEquals的测试仍然失败。我认为这是因为两个对象都在一个数组中,所以它实际上是比较一个对象数组和另一个对象数组。另外,我的实际对象是由函数构成的,而期望的对象是文字。

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function decorateClassListWithAges(classList) {
  return classList.reduce((arr, c) => {
    arr.push({
      'Name': c,
      'Age': getRandomIntInclusive(10, 10) //Currently always 10 for test purposes
    })
    return arr
  }, [])
}

function assertEquals(actual, expected, testName) {

  if (actual == null || typeof actual != 'object')
    return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

  if (Object.keys(actual).length !== Object.keys(expected).length)
    return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

     for (let k in actual)
       if (!expected.hasOwnProperty(k))
         return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

         for (let k in expected) {
           if (actual[k] !== expected[k])
             return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));
         }
  return console.log('passed');
}


let a = ['James', 'Paul']
let e = [{'Name' : 'James', 'Age' : 10}, {'Name' : 'Paul', 'Age' : 10}]
assertEquals(decorateClassListWithAges(a), e)

1 个答案:

答案 0 :(得分:2)

在这里,您尝试比较两个不同的对象:

for (let k in actual)
       if (!expected.hasOwnProperty(k))
         return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

         for (let k in expected) {
           if (actual[k] !== expected[k])
             return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));
         }

但是在JS中,如果两个对象引用完全相同的对象,则它们是相等的。

您可以使用lodash isEqual方法比较这两个对象:

if (!_.isEqual(actual[k], expected[k]))...

尝试下面的代码段:

 function getRandomIntInclusive(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function decorateClassListWithAges(classList) {
      return classList.reduce((arr, c) => {
        arr.push({
          'Name': c,
          'Age': getRandomIntInclusive(10, 10) //Currently always 10 for test purposes
        })
        return arr
      }, [])
    }

    function assertEquals(actual, expected, testName) {

      if (actual == null || typeof actual != 'object')
        return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

      if (Object.keys(actual).length !== Object.keys(expected).length)
        return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

         for (let k in actual)
           if (!expected.hasOwnProperty(k))
             return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));

             for (let k in expected) {
               if (!_.isEqual(actual[k], expected[k]))
                 return console.log('FAILED: expected ' + JSON.stringify(expected) + ' but got ' + JSON.stringify(actual));
             }
      return console.log('passed');
    }


    let a = ['James', 'Paul']
    let e = [{'Name' : 'James', 'Age' : 10}, {'Name' : 'Paul', 'Age' : 10}]
    assertEquals(decorateClassListWithAges(a), e)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>