gpt4 book ai didi

javascript - 我如何在 CucumberJS 中跨多个步骤定义文件共享我的 World 实例?

转载 作者:行者123 更新时间:2023-11-30 16:01:02 26 4
gpt4 key购买 nike

我正在实现一个 CucumberJS 场景,它在两个不同的步骤定义文件中使用多个步骤。第一步在世界上设置一些变量,其他步骤定义文件中的步骤必须使用这些变量。

变量设置正确,但是当其他文件上的步骤试图读取它时,它是未定义的。除了合并步骤定义文件之外,还有其他解决方法吗?

例子:

世界.js

var World = function World() {
this.client = '';
};

module.exports.World = World;

测试功能

Given a variable A
Then some other step

step1.steps.js

module.exports = function () {
this.World = require(process.cwd() + '/test/features/support/world').World;

this.Given(/^a Variable A$/, function () {
this.client = 'abc';
});
};

step2.steps.js

module.exports = function () {
this.World = require(process.cwd() + '/test/features/support/world').World;

this.Then(/^some other step$/, function () {
console.log(this.client);
});
};

最佳答案

您正在设置 this.client 而不是 this.World.client。此外,您应该在 world.js 中使用对象而不是构造函数:

世界.js

module.exports = {
client: ''
};

step1.steps.js

var world = require('./test/features/support/world.js');

module.exports = function () {
this.Given(/^a Variable A$/, function () {
world.client = 'abc';
});
};

step2.steps.js

var world = require('./test/features/support/world.js');

module.exports = function () {
this.Then(/^some other step$/, function () {
console.log(world.client);
});
};

关于javascript - 我如何在 CucumberJS 中跨多个步骤定义文件共享我的 World 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37748176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com