我正在尝试编写我认为是量角器中相当简单的测试,但是似乎在您尝试同步执行任何操作的那一刻,量角器使您的生活变得艰难!通常,处理定位器函数(返回promise)不是问题,因为任何expect语句都会在测试断言之前自动解析传递给它的任何promise语句。但是,我要做的事情涉及在Expect语句之前解决这些定位器诺言,以便我可以有条件地执行一些测试逻辑。考虑(伪代码):
// Imagine I have a number of possible elements on the page
// and I wish to know which are on the page before continuing with a test.
forEach(elementImLookingFor){
if (elementImLookingFor.isPresent) {
// record the fact that the element is (or isnt) present
}
}
// Now do something for the elements that were not found
但是,在我上面的示例中,“ isPresent”调用返回一个Promise,因此实际上不能以这种方式调用。将其作为promise调用(即,带有一个then)意味着如果该元素是否存在于页面上,我的forEach块将在记录之前退出。
我正在为此做一个空白,有人遇到过类似的事情吗?
我已经使用bluebird进行以下操作;
it('element should be present', function(done)
Promise.cast(elementImLookingFor.isPresent)
.then(function(present){
expect(present).toBeTruthy();
})
.nodeify(done);
});
如果您有几个要检查的元素,则isPresent
应该可以执行以下操作;
it('check all elements are present', function(done){
var promises = [element1, element2].map(function(elm){
return elm.isPresent();
});
// wait until all promises resolve
Promise.all(promises)
.then(function(presentValues){
// check that all resolved values is true
expect(presentValues.every(function(present){
return present;
})).toBeTruthy();
})
.nodeify(done);
});
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句