Assert array includes array with Chai

浪尽此生 提交于 2019-12-24 11:28:13

问题


I am trying evaluate whether data returned from a table (table.getData()) which is a 2D array contains another array.

In the console the expected data appears in the 2D array returned from the table.getData() call but the assertion fails.

this.Then(/^I see my account balances as follows:$/, function (tableData, done) {
        var balanceAggregationPage = new BalanceAggregationPage(this.app.pagesContainer),
            table = balanceAggregationPage.getAccountsTable();

        var rows = tableData.getRows();
        rows.shift();

        var actualBalances = [];
        rows.syncForEach(function (item) {
            var row = item.raw();
            row[7] = moment(parseInt(row[7], 10)).format('DD MMM YYYY hh:mm');

            actualBalances.push(row);
        });
        exp(table.getData()).to.eventually.include(actualBalances).notify(done);
    });

Can anyone help out? Thanks


回答1:


You may combine include with members().

Asserts that the target is a superset of set, or that the target and set have the same strictly-equal (===) members. Alternately, if the deep flag is set, set members are compared for deep equality.

Your assertion becomes:

exp(table.getData()).to.eventually.deep.include.members([actualBalances]);

Use the deep flag to ensure that chai will will compare the deep content of both arrays.

Also, this will work only if the order inside the arrays is the same. You may want to sort them.

I wrote a small example here that does what you want. It uses the 'should' interface instead of 'expect', but this does not matter.



来源:https://stackoverflow.com/questions/32335320/assert-array-includes-array-with-chai

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!