1) Если внутри функции шага нет асинхронного кода, нет необходимости использовать аргумент callback
.
Then(/^the response status is (.*)$/, function (status) {
// Synchronous code
assert.equal(this.responseStatus, status)
});
2) Но если внутри функции шага есть какой-либо асинхронный код, вынужно использовать аргумент callback
или вернуть обещание.
Например:
Given('This has only one word {string}', function (string, callback) {
// the `callback` is specified by argument: callback
... do other things
// when step done invoke the `callback`
callback()
}
Given('This has only one word {string}', function (string, abc) {
// the `callback` is specified by argument: abc
... do other things
// when step done invoke the `callback`
abc()
}
Given('This has only {amount} word {string}', function (amount, string, xyz) {
// the `callback` is specified by argument: xyz
... do other things
// when step done invoke the `callback`
xyz()
}
Важно : последний аргумент функции в Cucumber будет задан как callback
, независимо от того, какое имя аргумента вы дадитестрока.
// Asynchronous - callback
// Take a callback as an additional argument to execute when the step is done
Then(/^the file named (.*) is empty$/, function (fileName, callback) {
fs.readFile(fileName, 'utf8', function(error, contents) {
if (error) {
callback(error);
} else {
assert.equal(contents, '');
callback();
}
});
});
// Asynchronous - promise
// Return a promise. The step is done when the promise resolves or rejects
When(/^I view my profile$/, function () {
// Assuming this.driver is a selenium webdriver
return this.driver.findElement({css: '.profile-link'}).then(function(element) {
return element.click();
});
});
Вернуться к вашему коду:
Given('This has only one word {string}', function (string, callback) {
console.log(string);
// the `callback` specified by argument: callback
function callback() {}
// you define a function, named 'callback' too, which overwrite the
// real `callback`
callback();
// you invoked the function you defined, not the real `callback`,
// so cucumber wait the real `callback` be invoked, until reach
// the timeout, then report timeout exception.
});