承诺后运行规范已经解决 [英] Running spec after promise has been resolved
问题描述
我遇到了运行规范的问题,该规范应在承诺解决后执行。请参阅下面注释的简化示例。
I came across an issue with running a spec that should be executed after a promise has been resolved. See the commented simplified example below.
我尝试使用IIFE或在规范中调用 done()
函数但没有其中似乎有效。
I tried using IIFE or calling done()
function in the spec but none of these seemed to work.
// getIds() is a simple promise which returns an array of ids
getIds().then(function (ids) {
console.log('IDS: ' + ids); // all good so far
// This test is never run
it('dummy test', function () {
console.log('TEST HAS BEEN RUN');
});
});
推荐答案
您可以使用浏览器。 wait()
等待你的承诺完成。或者您可以将测试放在then块中:
You can use browser.wait()
to wait until your promise is complete. Or you can put your test inside the then block:
it('should test', function() {
getIds().then(function (ids) {
// some action.
expect()...
});
});
此外,您可以将承诺放在beforeEach或beforeAll(jasmine 2)中。将id分配给describe中声明的变量。该值应该可供您的测试使用。
Also, you can put the promise in a beforeEach or a beforeAll (jasmine 2). Assign the ids to a variable declared inside a describe. The value should be available for your test to use.
这篇关于承诺后运行规范已经解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!