这些方面的东西应该可以解决问题:
this.Given(/^blah$/, function (callback) {
var failed = false;
var timeout = setTimeout(function () {
callback(new Error("Timed out"));
}, 500);
doSomethingThatMightNeverCallback(function (err) {
if (!failed) {
clearTimeout(timeout);
callback(err);
}
});
});
您可以轻松地重新定义 Cucumber 的 Given/When/Then:
var cucumber = this;
var Given = When = Then = defineStep;
function defineStep (name, handler) {
cucumber.defineStep(name, function () {
var world = this;
var args = Array.prototype.splice.call(arguments, 0);
var callback = args.pop();
var failed = false;
var timeout = setTimeout(function () {
callback(new Error("Timed out"));
}, 500);
args.push(function (err) {
if (!failed) {
clearTimeout(timeout);
callback(err);
}
});
handler.apply(world, args);
});
}
Given()
然后使用,When()
而Then()
不是等定义您的步骤定义this.Given()
:
Given(/^foo$/, function (callback) {
setTimeout(callback, 400); // this will pass
});
Given(/^bar$/, function (callback) {
setTimeout(callback, 500); // this will fail
});
When(/^baz$/, function (callback) {
doYourStuff(callback);
});
正如预期的那样,我在步骤 2 中遇到了以下情况:
Feature: Baz
Scenario:
Given foo
And bar
输出:
cucumber.js test.feature
.F
(::) failed steps (::)
Error: Timed out
at null._onTimeout (/Users/jbpros/tmp/abc/code.js:13:18)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Failing scenarios:
/Users/jbpros/tmp/abc/test.feature:3 # Scenario:
1 scenario (1 failed)
2 steps (1 failed, 1 passed)
高温下,