Dennis Ferguson Dennis Ferguson - 2 years ago 106
AngularJS Question

adding variables together in expect statement

I have some strings that I have extracted using protractor/jasmine/angularJS and converted into integers. I am now trying to add these together and compare in an expect statement. But I am getting some promise errors in doing so.

var result0 = element.all(by.binding('Inboxes.Inbox.Count')).first().getText().then(parseFloat);
result0.then((value) => console.log("count: ", value));

var result1 = element.all(by.binding('InboxItem.Count')).get(0).getText().then(parseFloat);
result1.then((value) => console.log("count: ", value));

var result2 = element.all(by.binding('InboxItem.Count')).get(1).getText().then(parseFloat);
result2.then((value) => console.log("count: ", value));

var result3 = element.all(by.binding('InboxItem.Count')).get(2).getText().then(parseFloat);
result3.then((value) => console.log("count: ", value)).then(expect(result1 + result2 + result3).toEqual(result0));

//compare badge counts to Inbox badge count
expect(result1 + result2 + result3).toEqual(result0);

I am getting the following promise errors. I thought that since the promises had already been satisfied and the counts below print out (41, 7, 14 and 20) that I could add the bottom 3(reulst1-3) together and compare to result0 which is the total of result1-3. I am having a time with these promises since I am new to this and don't quite understand them very well.

count: 41
count: 7
count: 14
count: 20

1) Workflow Application When selecting Alerts panel should expand the Inbox panel and Postings
Expected 'ManagedPromise::859 {[[PromiseStatus]]: "pending"}ManagedPromise::896 {[[PromiseStatus]]: "pending"}ManagedPromise::933 {[[PromiseStatus]]: "pending"}' to equal ManagedPromise::822 {[[PromiseStatus]]: "pending"}.

Answer Source

As Mike already stated, you are trying to add promises together, not actual resolved values.

In this case, I would solve that using the protractor.promise.all() to resolve all the promises needed to make the expectation, at once:

protractor.promise.all([result0, result1, result2, result3]).then(function (values) {
    expect(values[1] + values[2] + values[3]).toEqual(values[0]);

And, to simplify that, you can spread() the resolved values into "then" function arguments:

protractor.promise.all([result0, result1, result2, result3]).then(spread(function (value0, value1, value2, value3) {
    expect(value1 + value2 + value3).toEqual(value0);

Note that, unlike q, protractor.promise does not have the spread() built-in and you have to have a custom one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download