在 Jasmine 1.3 runs/waitsFor 之后完成 Promises
Completing Promises after runs/waitsFor in Jasmine 1.3
我正在使用 jasmine-node 1.14.5,它在底层使用 jasmine 1.3,我在 runs/waitFor
与 Promises 正常工作时遇到了问题。
在某些测试中,我想 runs/waitFor
等待特定条件发生,当它发生时,履行我 return 返回的 Promise。但是,当我尝试构造一个传入 function(success, fail)
参数的 Promise 时,runs/waitFor
中代码的 none 被调用。但是,如果直接解析 Promise,它就可以工作。知道前一个选项不起作用吗?
举一些例子,下面的工作正常:
it("should support async execution of test preparation and expectations", function(done) {
var p = Promise.resolve("boo")
.then(function() {
var p2 = Promise.resolve("whatever");
runs(function() {
flag = false;
value = 0;
intId = setInterval(function() {
console.log(value);
if (++value == 3) { clearInterval(intId); flag = true; }
}, 500);
});
waitsFor(function() {
return flag;
}, "The Value should be incremented", 5000);
runs(function() {
expect(value).toEqual(3);
});
return p2;
});
p.then(function() {
done();
}).catch(function(err) {
done(err);
});
});
但另一方面,这不起作用,因为尽管调用 runs/waitsFor
没有问题,但内部的回调不会:
it("should support async execution of test preparation and expectations", function(done) {
var p = Promise.resolve("boo")
.then(function() {
return new Promise(function (fulfil, reject) {
runs(function () {
flag = false;
value = 0;
intId = setInterval(function () {
console.log(value);
if (++value == 3) {
clearInterval(intId);
flag = true;
}
}, 500);
});
waitsFor(function () {
return flag;
}, "The Value should be incremented", 5000);
runs(function () {
expect(value).toEqual(3);
fulfil();
});
});
});
p.then(function() {
done();
}).catch(function(err) {
done(err);
});
});
我也偶然尝试了以下方法,但也不起作用,它的行为方式与前面的示例相同:
it("should support async execution of test preparation and expectations", function(done) {
var p = Promise.resolve("boo")
.then(function() {
var outerFulfil;
var outerReject;
var p2 = new Promise(function(fulfil, reject) {
outerFulfil = fulfil;
outerReject = reject;
});
runs(function() {
flag = false;
value = 0;
intId = setInterval(function() {
console.log(value);
if (++value == 3) { clearInterval(intId); flag = true; }
}, 500);
});
waitsFor(function() {
return flag;
}, "The Value should be incremented", 5000);
runs(function() {
expect(value).toEqual(3);
outerFulfil();
});
return p2;
});
p.then(function() {
done();
}).catch(function(err) {
done(err);
});
});
知道如何解决吗?尽管第一个示例有效,但它的行为并不像我想要的那样,因为我只希望在执行 waitsFor
之后的断言后履行承诺。
干杯,
高尔德
我最终完全放弃了 runs/waitsFor,转而使用基于时间的递归 Promise 循环,例如
function waitUntil(expectF, cond, op) {
var now = new Date().getTime();
function done(actual) {
return cond(actual)
&& new Date().getTime() < now + MAX_WAIT;
}
function loop(promise) {
exports.sleepFor(100); // brief sleep
// Simple recursive loop until condition has been met
return promise
.then(function(response) {
return !done(response)
? loop(op())
: response;
})
.catch(function() {
return loop(op());
});
}
return loop(op())
.then(function(actual) {
expectF(actual);
});
}
op
是一个操作,returns一个Promise。
例如,我想等到一定数量的节点加入集群:
function waitUntilView(expectNumMembers, nodeName) {
return waitUntil(
function(members) { expect(members.length).toEqual(expectNumMembers); },
function(members) { return _.isEqual(expectNumMembers, members.length); },
getClusterMembers(nodeName)
);
}
我正在使用 jasmine-node 1.14.5,它在底层使用 jasmine 1.3,我在 runs/waitFor
与 Promises 正常工作时遇到了问题。
在某些测试中,我想 runs/waitFor
等待特定条件发生,当它发生时,履行我 return 返回的 Promise。但是,当我尝试构造一个传入 function(success, fail)
参数的 Promise 时,runs/waitFor
中代码的 none 被调用。但是,如果直接解析 Promise,它就可以工作。知道前一个选项不起作用吗?
举一些例子,下面的工作正常:
it("should support async execution of test preparation and expectations", function(done) {
var p = Promise.resolve("boo")
.then(function() {
var p2 = Promise.resolve("whatever");
runs(function() {
flag = false;
value = 0;
intId = setInterval(function() {
console.log(value);
if (++value == 3) { clearInterval(intId); flag = true; }
}, 500);
});
waitsFor(function() {
return flag;
}, "The Value should be incremented", 5000);
runs(function() {
expect(value).toEqual(3);
});
return p2;
});
p.then(function() {
done();
}).catch(function(err) {
done(err);
});
});
但另一方面,这不起作用,因为尽管调用 runs/waitsFor
没有问题,但内部的回调不会:
it("should support async execution of test preparation and expectations", function(done) {
var p = Promise.resolve("boo")
.then(function() {
return new Promise(function (fulfil, reject) {
runs(function () {
flag = false;
value = 0;
intId = setInterval(function () {
console.log(value);
if (++value == 3) {
clearInterval(intId);
flag = true;
}
}, 500);
});
waitsFor(function () {
return flag;
}, "The Value should be incremented", 5000);
runs(function () {
expect(value).toEqual(3);
fulfil();
});
});
});
p.then(function() {
done();
}).catch(function(err) {
done(err);
});
});
我也偶然尝试了以下方法,但也不起作用,它的行为方式与前面的示例相同:
it("should support async execution of test preparation and expectations", function(done) {
var p = Promise.resolve("boo")
.then(function() {
var outerFulfil;
var outerReject;
var p2 = new Promise(function(fulfil, reject) {
outerFulfil = fulfil;
outerReject = reject;
});
runs(function() {
flag = false;
value = 0;
intId = setInterval(function() {
console.log(value);
if (++value == 3) { clearInterval(intId); flag = true; }
}, 500);
});
waitsFor(function() {
return flag;
}, "The Value should be incremented", 5000);
runs(function() {
expect(value).toEqual(3);
outerFulfil();
});
return p2;
});
p.then(function() {
done();
}).catch(function(err) {
done(err);
});
});
知道如何解决吗?尽管第一个示例有效,但它的行为并不像我想要的那样,因为我只希望在执行 waitsFor
之后的断言后履行承诺。
干杯, 高尔德
我最终完全放弃了 runs/waitsFor,转而使用基于时间的递归 Promise 循环,例如
function waitUntil(expectF, cond, op) {
var now = new Date().getTime();
function done(actual) {
return cond(actual)
&& new Date().getTime() < now + MAX_WAIT;
}
function loop(promise) {
exports.sleepFor(100); // brief sleep
// Simple recursive loop until condition has been met
return promise
.then(function(response) {
return !done(response)
? loop(op())
: response;
})
.catch(function() {
return loop(op());
});
}
return loop(op())
.then(function(actual) {
expectF(actual);
});
}
op
是一个操作,returns一个Promise。
例如,我想等到一定数量的节点加入集群:
function waitUntilView(expectNumMembers, nodeName) {
return waitUntil(
function(members) { expect(members.length).toEqual(expectNumMembers); },
function(members) { return _.isEqual(expectNumMembers, members.length); },
getClusterMembers(nodeName)
);
}