繁体   English   中英

AssertionError:无法比较节点js中的两个对象

[英]AssertionError : Unable to compare two objects in node js

我正在使用以下函数从应用程序中获取值并将其与预期值进行比较。但是以下输出失败。请帮我解决这个问题。

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

}

Output:

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 function 返回的内容。

进行以下更改后问题已解决:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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