问题
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