我有一些使用 Protractor/jasmine/angularJS 提取的字符串并将其转换为整数。我现在尝试将它们加在一起并在期望语句中进行比较。但我这样做时遇到了一些 promise 错误。
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);
});
});
});
我收到以下 promise 错误。我认为,既然 promise 已经得到满足,并且下面的计数打印出来(41、7、14 和 20),我可以将底部 3(reulst1-3)加在一起,并与 result0 进行比较,result0 是 result1-3 的总和。我对这些 promise 很感兴趣,因为我对此很陌生并且不太理解它们。
Started
count: 41
count: 7
count: 14
count: 20
F
Failures:
1) Workflow Application When selecting Alerts panel should expand the Inbox panel and Postings
Message:
Expected 'ManagedPromise::859 {[[PromiseStatus]]: "pending"}ManagedPromise::896 {[[PromiseStatus]]: "pending"}ManagedPromise::933 {[[PromiseStatus]]: "pending"}' to equal ManagedPromise::822 {[[PromiseStatus]]: "pending"}.
最佳答案
您尝试将 promise 添加在一起,而不是实际解析的值。
在这种情况下,我将使用 protractor.promise.all()
一次性解决实现期望所需的所有 promise :
protractor.promise.all([result0, result1, result2, result3]).then(function (values) {
expect(values[1] + values[2] + values[3]).toEqual(values[0]);
});
<小时/>
而且,为了简化这一点,您可以 spread()
the resolved values进入“then”函数参数:
protractor.promise.all([result0, result1, result2, result3]).then(spread(function (value0, value1, value2, value3) {
expect(value1 + value2 + value3).toEqual(value0);
}));
请注意,与 q
不同,protractor.promise
没有内置 spread()
,您必须有一个自定义的一个。
关于angularjs - 在expect语句中添加变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665308/