AssertionError : Unable to compare two objects in node js

AssertionError : Unable to compare two objects in node js

我正在使用以下函数从应用程序中获取值并将其与预期的值进行比较value.But它失败了以下outpupt.Please帮助我解决这个问题。

getEleAttribute = async function(ele, attr) {

    var attribute = await ele.getAttribute(attr);
    return attribute;

}

enterTodayDate= function(){
    return moment(Date.now()).format('YYYY-MM-DD');
   }

this.verifyProp = async function () {
   var fetchDate = getEleAttribute(verifyDate, "value").toString().substring(0, 10);
   console.log(fetchDate); // this is printing -- [object Pr
   var today=enterTodayDate().toString();
   assert.equal(fetchDate, today, "Expected Date is not same as Actual Date");

}

输出:

AssertionError: Expected Date is not same as Actual Date: expected '[object Pr' to equal '2020-06-18'
           + expected - actual

           -[object Pr
           +2020-06-18

代码看起来不错,但你的逻辑有问题。

调用 getEleAttribute(verifyDate, "value").toString() 返回对象的名称,而不是其中包含日期的字符串。当您将断言与使用 moment 方法生成的日期字符串进行比较时,断言失败。

你需要调试代码看看getEleAttribute函数是什么returns。

以下更改后问题已解决:

var fetchDate = getEleAttribute(verifyDate, "value");
var actualDate= String(fetchDate).substr(0, 10);
console.log(actualDate); //printed -- 2020-06-19