2

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

Joly
  • 3,218
  • 14
  • 44
  • 70

1 Answers1

4

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.

nioKi
  • 1,259
  • 9
  • 17
  • This is actually very good. I didn't knew that. I'm deleting my answer in favor of your's. Thanks! – Alfonso Presa Sep 01 '15 at 16:12
  • Doesn't work for me unfortunately with my data which is passed as cucumber TableData object i'm getting: thenCatch: [Function], thenFinally: [Function], addCallback_: [Function] } to be an array – Joly Sep 02 '15 at 09:53
  • Oh, I was misled by the fact that you were talking about arrays in your question. Indeed, if your data is an object & not an array as said, it won't work as **include only works on strings or arrays**. Sadly, I don't know Cucumber at all, so I don't really know how it works. Maybe you can convert your TableData object to a *real* array ? – nioKi Sep 02 '15 at 10:17
  • but table.GetData() is the asynchornous call from chai, I need to run the comparison on it, unless I'm missing something.... – Joly Sep 02 '15 at 10:35
  • If it's asynchronous, you just have to add .eventually in the assertion chain (I updated my answer, see the line of code). And if table.getData() asynchronous answer is not an array, the assertion won't work, you'll have to transform it or change it. This means you will have to get the result of the asynchronous call (and not test it directly in the assertion), transform it, then test it. – nioKi Sep 02 '15 at 11:41
  • Thanks. Will you be able to elaborate this with a code example perhaps? – Joly Sep 02 '15 at 13:05
  • Here you go: http://jsfiddle.net/V34cb/6/ You need to be familiar with promises. I don't really know if its relevant though, because I don't know at all what a cucumber TableData object looks like, but it may helps you. Moreover, this goes beyond the scope of the original question. You may want to ask another, more precise question on Cucumber and Chai. If so, I'm deeply sorry, but I won't be able to help you, as Cucumber is a total mystery for me. – nioKi Sep 02 '15 at 13:56