2017-09-18 18 views
0

두 객체를 비교하려고합니다. 두 객체가 동일한 키를 가지고 있고, 그 키 값이 같은 경우에도 assertEquals의 테스트가 여전히 실패합니다. 나는 그것이 두 개의 객체가 배열 내부에 있기 때문에 실제로 객체의 다른 배열과 다른 배열을 비교하고 있기 때문이라고 생각합니다. 또한 실제 객체는 함수에서 만들어지며 예상 객체는 리터럴입니다.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) 

답변

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에서 두 객체가 동일한

.

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>
좋은 생각

+0

하지만 내가들은이 캐릭터 라인 화 경우에만 작동

당신은 isEqual 방법을 lodash 사용하여이 두 개체를 비교할 수 있습니다 객체 키는 같은 순서로 존재합니다.이 경우에는 그렇습니다. 그러나 여전히 문자열에 의존하지 않는 비교를 알고 싶습니다. 미래. 고맙습니다 – jhazelton1