我想写一个这样的功能:
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技能,这应该是非常可行的,我只是搞砸了回调?
答案 0 :(得分:0)
首先,我说你的重写功能是错误的。你永远不应该回到渐进的给定,何时,然后。你将从Then回到When,这是错误的。
给定用于设置前置条件。何时用于实际测试。然后用于断言。每个场景应该是一个单独的测试,因此应该只有很少的When子句。如果需要,可以使用Scenario Outlines将几个非常相似的测试混合在一起。
在这种情况下,建议将其恢复到第一原则并查看是否有效。然后慢慢积累起来工作。
我怀疑在这种情况下,问题出现在一些未被处理的例外情况中。您可以尝试重写它以使用promises,然后在出错时拒绝。这样可以提供更好的错误报告。