würde Ich mag eine Funktion wie diese schreiben:Gurke Js Rückruf Problem? oder Feature-Problem?
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
, die Schritte wie diese binden würde:
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();
// });
});
Das Problem, das ich habe ist, dass ich tun kann, nichts im Then Callback. An dieser Stelle möchte ich überprüfen können, ob die Antwort die richtigen Daten enthält.
Hier sind relevante Auszüge aus den Hilfsdateien:
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();
});
}
Etwas, das wir betrachten, als eine Option ist die Funktion neu zu schreiben, eine andere Schrittdefinition Struktur zu passen. Wenn wir wieder schrieb die Funktion wie folgt aus:
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
Ich glaube, es den Rist Rückruf Problem umgeht wir ringen mit, aber ich hasse es, den Zusammenbruch der Funktion. Dadurch wird das Feature für das Unternehmen weniger lesbar und verständlich.
Also ... meine Frage, die Zusammenfassung zuerst präsentiert, ist es falsch geschrieben? Versuche ich, Schrittdefinitionen zu bekommen, um etwas zu tun, was sie nicht tun sollten? Oder ist mein Mangel an Js Fähigkeiten hell, und das sollte sehr machbar sein, ich mische nur die Rückrufe?
Graham danke für die Antwort! Ich wrestle mit ein paar Gedanken: Erstens, sollte die Wann immer/nur die Anfrage sein? Und die Then-Handles werten das Feedback von diesem When/request aus? Wenn dies der Fall wäre, würde das Szenario, das wir zu testen versuchen, dem Muster WhenThenWhenThen ... folgen (was ich hasse). Was ich wollte, war ein Then, das blockiert und mehrere Ruhe-Anfragen macht, um die Daten zu validieren, die im When generiert wurden. Ist das koscher? Kann ich eine Blockierung haben? Dann macht das mehrere externe Anfragen? Ich schätze das Feedback sehr! –