我一直在尝试为 API 执行一些测试,对于这个示例,它需要更新我的帐户详细信息。当我运行测试时,retrieveAccount 调用有时会在我的 putRequest 使测试失败之前运行。我究竟做错了什么?
var frisby = require('frisby');
var url = require('endpoints.js');
var auth = require('auth.js');
var oracledb = require('oracledb');
var dbConnect = require('dbconfig.js');
var myDetails = undefined;
var putRequest = function() {
frisby.create('Put update contact details - required values')
.put(url.myAccount, {
addressLine1: 'String',
addressTown: 'String',
addressCounty: 'String'
}, {json: true})
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.auth(auth.username, auth.password)
.toss();
}
var retrieveAccount = function() {
oracledb.getConnection(
{
user : dbConnect.user,
password : dbConnect.password,
connectString : dbConnect.connectString
},
function(err, connection)
{
if (err) {
console.error(err.message);
return;
}
connection.execute(
"SELECT addressLine1, addressTown, addressCounty "
+ "FROM accounts "
+ "WHERE account_id = 1",
function(err, result)
{
if (err) {
console.error(err.message);
return;
}
myDetails = JSON.stringify(result.rows);
myDetails = JSON.parse(myDetails);
});
});
}
var matchValues = function() {
frisby.create('Match Database and API Values')
.get(url.myAccount)
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.auth(auth.username1, auth.password1)
.afterJSON(function (body) {
expect(body.addressLine1).toMatch(myDetails[0][0])
expect(body.addressCounty).toMatch(myDetails[0][1])
expect(body.addressTown).toMatch(myDetails[0][0])
})
.toss();
}
function Promise(fn) {
var state = 'pending';
var value;
var deferred = null;
function resolve(newValue) {
value = newValue;
state = 'resolved';
if(deferred) {
handle(deferred);
}
}
function handle(handler) {
if(state === 'pending') {
deferred = handler;
return;
}
if(!handler.onResolved) {
handler.resolve(value);
return;
}
var ret = handler.onResolved(value);
handler.resolve(ret);
}
this.then = function(onResolved) {
return new Promise(function(resolve) {
handle({
onResolved: onResolved,
resolve: resolve
});
});
};
fn(resolve);
}
function sendRequest() {
return new Promise(function(resolve){
var value = putRequest();
resolve(value);
});
}
function readDatabase() {
return new Promise(function(resolve){
var value = retrieveAccount();
resolve(value);
});
}
function getAccount() {
return new Promise(function(resolve){
var value = matchValues();
resolve(value);
});
}
sendRequest()
.then(readDatabase)
.then(getAccount);