我正在实现一个CucumberJS场景,它在两个不同的步骤定义文件中使用多个步骤。第一步是在World上设置一些变量,这些变量必须由另一个步骤定义文件中的步骤使用。
变量设置正确,但当另一个文件上的步骤尝试读取它时,它是未定义的。除了合并步骤定义文件之外,还有任何想法如何解决这个问题?
示例:
world.js
var World = function World() {
this.client = '';
};
module.exports.World = World;
test.feature
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);
});
};
答案 0 :(得分:1)
You are setting this.client
instead of this.World.client
. Moreover you should use an object and not a constructor in world.js
:
world.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);
});
};
答案 1 :(得分:0)
您可以直接参数化您的test.feature:
Given a variable "abc"
Then some other step
现在在您的步骤中传递此变量
step1.steps.js
module.exports = function() {
this.World = require(process.cwd() + '/test/features/support/world').World;
this.Given(/^a Variable "([^"]*)"$/, function(variable) {
this.client = variable;
});
};
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); // would print abc
});
};