茉莉花添加到承诺

Jasmine Add To Promise

我 运行 关注这个问题:

Expected 2 to equal 'Promise::6492 {[[PromiseStatus]]: "pending"}1'.

我的代码如下所示:

规格:

this.allRows = element.all(by.name('row'));

茉莉花

var prevRowCount = this.allRows().count()

this.addRow();

expect(this.allRows.count()).toEqual(prevRowCount+1);

如何将 this.allRows.count() returns 的计数加一?

您必须明确解决 prevRowCount 承诺:

prevRowCount.then(function (prevRowCount) {
    this.addRow();

    expect(this.allRows.count()).toEqual(prevRowCount+1);
});

请注意,您不需要对 this.allRows.count() 中新获得的行数执行相同的操作,因为 expect() 会在检查预期之前隐式解析它。

量角器的 ElementFinder and ElementArrayFinder are built around Selenium's WebElement, thus they rely on promises heavily.

Protractor provides an adapter for Jasmine,因此后者可以拥抱它的承诺性质,并在 expect 和匹配器中透明地展开承诺。

所以规范应该坚持手工解包承诺。或者可以直接匹配promise。它成为 ES5 的一项微不足道的任务:

expect(this.allRows.count()).toEqual(prevRowCount.then(function (count) {
  return ++count;
}));

而且用ES6语法糖更方便:

expect(this.allRows.count()).toEqual(prevRowCount.then((count) => ++count));