Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Cucumber JS:如何导出/更新给定/When/Then步骤之外的全局变量?_Javascript_Cucumberjs - Fatal编程技术网

Javascript Cucumber JS:如何导出/更新给定/When/Then步骤之外的全局变量?

Javascript Cucumber JS:如何导出/更新给定/When/Then步骤之外的全局变量?,javascript,cucumberjs,Javascript,Cucumberjs,我尝试更新/导出全局变量firstString,以便在“Then”步骤中使用和验证它。 如何正确导出它?当我这样做时,第一个字符串是未定义的。 仅当我在步骤内导出/导入它时,它才起作用。如何全局更新它并在“Then”文件中使用它 helpers.js: let firstString; give.js: let { firstString } = require('./helpers') Given('I have first {string}', function (stringValue

我尝试更新/导出全局变量firstString,以便在“Then”步骤中使用和验证它。 如何正确导出它?当我这样做时,第一个字符串是未定义的。 仅当我在步骤内导出/导入它时,它才起作用。如何全局更新它并在“Then”文件中使用它

helpers.js:

let firstString;
give.js:

let { firstString } = require('./helpers')

Given('I have first {string}', function (stringValue) {
    return stringRequest(stringValue).then(response => {
        firstString = response
    });
});

module.exports = { firstString }
那么.js:

firstString = require('./helpers').firstString
Then('blablabla {string}', function (stringType) {
    console.log(firstString)
});

如果我对您想要做什么的理解是正确的,那么您希望跨步骤存储数据。您需要使用Cumber为您提供的世界实例。您可以通过this关键字分步访问world实例

所以你要做的是

Given('I have first {string}', function (stringValue) {
return stringRequest(stringValue).then(response => {
    this.firstString = response
});
})


有关world实例的更多信息,请查看

如果我正确理解了您要做的事情,那么您希望跨步骤存储数据。您需要使用Cumber为您提供的世界实例。您可以通过this关键字分步访问world实例

所以你要做的是

Given('I have first {string}', function (stringValue) {
return stringRequest(stringValue).then(response => {
    this.firstString = response
});
})


有关world实例的更多信息,请查看

我已将其添加到我的world函数中。前缀。@HuckleberryCarignan是的,这是这个答案中提出的解决方案。若你们相信答案是正确的,请向上投票。我已经用这个在我的世界函数中添加了它。前缀。@HuckleberryCarignan是的,这是这个答案中提出的解决方案。如果你认为答案正确,请投赞成票