简体   繁体   中英

Object Equality in assertEquals

I am trying to compare 2 objects, first by if they have the same keys, then by if their key values are equal, yet the tests from assertEquals are still failing. I think it's because the two objects are inside of an array, so it's actually comparing one array of objects vs. another array of objects. Also, my actual object is made from a function whereas the expected object is a literal.

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)

Here you try to compare two different objects:

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));
         }

But in JS two objects are equal if they refer to the exact same object.

You can compare this two objects using lodash isEqual method:

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

Try snippet bellow:

  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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM