首页 > 解决方案 > 在同一场景中的不同步骤文件之间共享数据

问题描述

鉴于这个简单的情景

  Scenario: checkout the response code after foo data request 

    I request foo data
    Then the response code is 200 

在我的 foo 步骤文件中,我编写了一个进行 api 调用的步骤:

   When(/^I request foo data$/, (callback) => {
        ...
        apiCall().then((response) => {
            ...
            this.responseStatus = response.statusCode;
            callback();
        })
    });

在我的通用步骤文件中,我想放置共享步骤,例如:

Then(/^the response code is (\d+)$/, function (responseCode) {
    assert.equal(responseCode, this.responseStatus);
});

但问题是当我尝试运行它时,我得到了:

this对象显然没有共享,我得到了:

AssertionError [ERR_ASSERTION]: 200 == undefined

如果我将代码移动到同一个文件,它就可以工作!

那么如何用不同的文件解决这个问题呢?

标签: cucumbercucumberjs

解决方案


我们这样做的一种方法是在两个文件中都可以访问一个全局变量。类似于常量的东西,它在您的 WHEN 步骤中被初始化然后重新分配。这可以稍后在您的 THEN 文件中使用。


推荐阅读