我尝试更新/导出全局变量(firstString)以在“Then”步骤中使用和验证它。 如何正确导出?当我这样做时,第一个字符串是未定义的。 仅当我在步骤内导出/导入它时它才有效。如何全局更新它并在“Then”文件中使用它?
helpers.js:
let firstString;
给定的.js:
let { firstString } = require('./helpers')
Given('I have first {string}', function (stringValue) {
return stringRequest(stringValue).then(response => {
firstString = response
});
});
module.exports = { firstString }
then.js:
firstString = require('./helpers').firstString
Then('blablabla {string}', function (stringType) {
console.log(firstString)
});
最佳答案
如果我对您想要执行的操作的理解是正确的,那么您希望跨步骤存储数据。 Todo,您将需要使用 Cucumber 为您提供的世界实例。您可以通过 this
关键字逐步访问世界实例。
所以你会做的是
Given('I have first {string}', function (stringValue) {
return stringRequest(stringValue).then(response => {
this.firstString = response
});
});
Then('blablabla {string}', function (stringType) {
console.log(this.firstString)
});
有关世界实例的更多信息,请查看 https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/world.md
关于javascript - cucumber JS : How to export/update global variable outside Given/When/Then step?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58075190/