gpt4 book ai didi

javascript - assertEquals 中的对象相等

转载 作者:行者123 更新时间:2023-12-01 03:04:48 26 4
gpt4 key购买 nike

我试图比较两个对象,首先比较它们是否具有相同的键,然后比较它们的键值是否相等,但来自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)

最佳答案

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

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>

关于javascript - assertEquals 中的对象相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46274911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com