cucumber - 在同一场景中的不同步骤文件之间共享数据
问题描述
鉴于这个简单的情景
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
如果我将代码移动到同一个文件,它就可以工作!
那么如何用不同的文件解决这个问题呢?
解决方案
我们这样做的一种方法是在两个文件中都可以访问一个全局变量。类似于常量的东西,它在您的 WHEN 步骤中被初始化然后重新分配。这可以稍后在您的 THEN 文件中使用。
推荐阅读
- regex - 在模式之后匹配单词并使用sed在文件中附加后缀?
- php - 如何找出 HH:MM 的当前时间是否在两个区间 HH:MM - HH:MM
- amazon-web-services - AWS Codedeploy 没有这样的文件或目录
- html - 用border-radius绘制理想的对称css边框
- mysql - 数据库设计:循环参考以及如何更正它
- php - 在 PHP 中使用 &$variable 时内存中会发生什么?
- spring-boot - 无法使用 AbstractReactiveElasticsearchConfiguration 将数据写入 ElasticSearch
- r - 通过索引选择 data.table 列的问题
- c++ - OpenGL - 在一个函数中加载多个纹理
- java - CBC 和 CFB 模式下的 Java AES 未正确加密?四不尊重?