簡體   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