Как мне поделиться своим экземпляром World с несколькими файлами определения шагов в CucumberJS?

Я реализую сценарий 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);
    });
};

person Dragnipur    schedule 10.06.2016    source источник


Ответы (2)


Вы устанавливаете 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);
    });
};
person Florent B.    schedule 10.06.2016
comment
Это действительно помогло, хотя и отличается от официальной документации. спасибо! - person Dragnipur; 13.06.2016
comment
@Florent B. stackoverflow.com/questions/48297950/ - person Valay; 17.01.2018
comment
@Florent B. как получить одинаковое значение для Date.now() для разных функций? - person Valay; 17.01.2018

Вы можете напрямую параметризовать свой 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
  });
};
person Ram Pasala    schedule 10.06.2016