黄瓜 Js 回调问题? 还是功能问题?



我想写一个这样的功能:

Scenario: new Singleton create
When a new, unmatchable identity is received
Then a new tin record should be created
And a new bronze record should be created
And a new gold record should be created

这将与这样的步骤相关联:

defineSupportCode(function ({ Before, Given, Then, When }) {
var expect = require('chai').expect;
var chanceGenerator = require('./helpers/chanceGenerator')
var request = require('./helpers/requestGenerator')
let identMap;
// reset identMap before each scenario 
Before(function () {
identMap = [];
});
// should generate a valid identity
// persist it in a local variable so it can be tested in later steps
// and persist to the db via public endpoint
When('a new, unmatchable identity is received', function (callback) {
identMap.push(chanceGenerator.identity());
request.pubPostIdentity(identMap[identMap.length-1], callback);
});
// use the local variable to retrieve Tin that was persisted
// validate the tin persisted all the props that it should have
Then('a new tin record should be created', function (callback) {
request.pubGetIdentity(identMap[identMap.length-1], callback);
// var self = this;
// request.pubGetIdentity(identMap[identMap.length-1], callback, () => {
//   console.log('never gets here...');
//   self.callback();
//   callback();
// });
// request.pubGetIdentity(identMap[identMap.length-1], (callback) => {
//   console.log('never gets here...');
//   self.callback();
//   callback();
// });
});

我遇到的问题是我无法在 Then 回调中执行任何操作。 这就是我希望能够验证响应具有正确数据的地方。

以下是帮助程序文件的相关摘录:

var pubPostIdentity = function (ident, callback) {
console.log('pubIdentity');
var options = {
method: 'POST',
url: 'http://cucumber.utu.ai:4020/identity/' + ident.platform + '/' + ident.platformId,
headers: {
'X-Consumer-Custom-Id': ident.botId + '_' + ident.botId
},
body: JSON.stringify(ident)
};
console.log('ident: ', ident);
request(options, (err, response, body) => {
if (err) {
console.log('pubPostIdentity: ', err);
callback(err);
}
console.log('pubPostIdentity: ', response.statusCode);
callback();
});
}
// accept an identity and retrieve from staging via identity public endpoint
var pubGetIdentity = function (ident, callback) {
console.log('pubGetIdentity');
var options = {
method: 'GET',
url: 'http://cucumber.utu.ai:4020/identity/' + ident.platform + '/' + ident.platformId,
headers: {
'X-Consumer-Custom-Id': ident.botId + '_' + ident.botId
}
};
request(options, (err, response) => {
if (err) {
console.log('pubGetIdentity: ', err);
callback(err);
}
console.log('pubGetIdentity: ', response.body);
callback();
});
}

我们正在考虑的一种选择是重写功能以适应不同的步骤定义结构。 如果我们像这样重写功能:

Scenario: new Singleton create
When a new, unmatchable 'TIN_RECORD' is received
Then the Identity Record should be created successfully
When the Identity Record is retreived for 'tin'
Then a new 'tin' should be created
When the Identity Record is retreived for 'bronze'
Then a new 'bronze' should be created
When the Identity Record is retreived for 'gold'
Then a new 'gold' should be created

我相信它绕过了我们正在解决的脚背回调问题,但我真的很讨厌该功能的故障。 它使该功能对业务的可读性和可理解性降低。

那么......我的问题,首先呈现的摘要功能,是不是写错了? 我是否试图让步骤定义做一些他们不应该做的事情? 还是我缺乏 Js 技能闪耀,这应该是非常可行的,我只是搞砸了回调?

首先,我会说你重写的功能是错误的。你永远不应该回到给定,何时,然后的进展中。你从"然后"回到"时间",这是错误的。

给定用于设置前提条件。何时用于实际测试。然后用于断言。每个场景都应该是一个测试,因此应该有很少的 When 子句。如果需要,可以使用方案大纲将几个非常相似的测试混合在一起。

在这种情况下,建议将其带回第一原则,看看是否有效。然后慢慢建立起来,出去工作。

我怀疑在这种情况下,问题出在某个未处理的异常中。您可以尝试重写它以改用承诺,然后在错误时被拒绝。这样可以提供更好的错误报告。

最新更新