未能通过断言测试JS对象读取为[对象对象]而不是实际对象值

时间:2018-03-01 00:35:07

标签: javascript object assertion

我目前正在尝试为JS中的函数创建我的第三个断言测试。出于某种原因,实际的'我传递给断言测试的值被读作' [object Object]'而不是我传递给函数的对象中实际存在的值。我不知道为什么会发生这种情况(JS新手)...我必须通过JSON将其更改为字符串时进行断言测试以读取实际对象,但因为我需要比较对象的KEYS,对我帮助不大。我可以参考我应该做什么或者阅读什么来解决这个问题?

我的断言功能:

function assertObjProperties(actual,expected,testName){
  if(Object.keys(actual) === Object.keys(expected)){
     console.log(`Passed. ${testName}`);
     } else {
       console.log(`Fail ${testName}. Expected ${expected}, but got ${actual}`);
     }
}

我对断言测试的调用:

assertObjProperties(input[0], classListWithAges[0] , 'it contains the correct keys \n');

我的输入'和' classListWithAges' :

var classListWithAges = [
  {"name":"Joe","age":11},
  {"name":"Jack","age":10},
  {"name":"John","age":11},
  {"name":"Fred","age":11},
  {"name":"Frank","age":11}
];


var input = [
  {"name":"Joe","age":11},
  {"name":"Jack","age":10},
  {"name":"John","age":11},
  {"name":"Fred","age":11},
  {"name":"Frank","age":11}
];

控制台返回的内容:

Fail it contains the correct keys 
. Expected [object Object], but got [object Object]   // here the issue :/

0 个答案:

没有答案